brianrowe
10/28/2014 - 2:19 AM

ajaxData.cfm

<cfscript>
// Save this file to a path such as /{SiteID}/includes/remote.cfc
component {

	remote string function getData(siteid='default', contentid='00000000000000000000000000000000001') returnformat='plain' {
		var str = '';
		var $ = get$(arguments.siteid, arguments.contentid);

		savecontent variable='str' {
			WriteOutput("<h1>Hello from remote.cfc</h1>
				<p>You're currently viewing <strong>#$.content('title')#</strong>.<br />
				The content type is <strong>#$.content('type')#</strong>.</p>");
		};

		return str;
	}

	private any function get$(siteid='default', contentid='00000000000000000000000000000000001') {
		var $ = application.serviceFactory.getBean('$').init(arguments.siteid);
		// Just in case we need access to the ContentBean
		var cBean = $.getBean('content').loadBy(contentid=arguments.contentid);
		$.setContentBean(cBean);
		return $;
	}

}
</cfscript>
<!--- You could place this somewhere on a layout template --->

<!--- This one gets data from ajaxData.cfm --->
<script>
jQuery(document).ready(function ($){
  // we're passing the siteid + contentid via URL so that the ajax file will know what site we're using and what content item we're on.
  // learn more about $.get() :: http://api.jquery.com/jquery.get/
	$.get('#$.siteConfig('assetPath')#/includes/remote/ajaxData.cfm?siteid=#$.event('siteid')#&contentid=#$.content('contentid')#', function(data){
		$('.cfm-result').html(data);
	});
});
</script>
<!--- The HTML returned from ajaxData.cfm will appear in this div --->
<div class="cfm-result"></div>

<!--- This one gets data from remote.cfc --->
<script>
jQuery(document).ready(function ($){
	$.get('#$.siteConfig('assetPath')#/includes/remote.cfc?method=getData&siteid=#$.event('siteid')#&contentid=#$.content('contentid')#', function(data){
		$('.cfc-result').html(data);
	});
});
</script>
<div class="cfc-result"></div>
<cfscript>
  // Save this file to a path such as /{SiteID}/includes/remote/ajaxData.cfm
	// This allows you to pass in the siteid and contentid via URL params
	param name='url.siteid' default='default';
	param name='url.contentid' default='00000000000000000000000000000000001';
	// Mura $cope
	$ = application.serviceFactory.getBean('$').init(url.siteid);
	// Just in case we need access to the ContentBean
	cBean = $.getBean('content').loadBy(contentid=url.contentid);
	$.setContentBean(cBean);
</cfscript>
<cfoutput>
	<h1>Hello from ajaxData.cfm</h1>
	<p>You're currently viewing <strong>#$.content('title')#</strong>.<br />
	The content type is <strong>#$.content('type')#</strong>.</p>
</cfoutput>