Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trycf/be91588a68dc522c5c5ecba4b7602368 to your computer and use it in GitHub Desktop.
Save trycf/be91588a68dc522c5c5ecba4b7602368 to your computer and use it in GitHub Desktop.
TryCF Gist
<cfdump var="#getDataForJSVersion()#" label="New Example">
<cfdump var="#getDataForJSVersionOld()#" label="Original JSON">
<cffunction name="getPublicTotals" returntype="array">
<cfargument name="year" type="numeric" required="true">
<!--- Hard coded query for DEMO ONLY --->
<cfset local.qResults = queryNew("Y1,Y2,Y3,Y4,Y5,Y6", "integer,integer,integer,integer,integer,integer"
, [1,2,3,4,5,6])>
<cfset local.data = [ local.qResults.Y1
, local.qResults.Y2
, local.qResults.Y3
, local.qResults.Y4
, local.qResults.Y5
, local.qResults.Y6
]>
<cfreturn local.data>
</cffunction>
<cfscript>
// NEW FUNCTION
remote struct function getDataForJSVersion() returnformat="json" {
// build array of structures for the desired range of years, ie. 2006 to 2010
local.records = [];
for (local.year = 2006; local.year <= 2010; local.year++) {
arrayAppend(local.records, {"text" = "Fall "& local.year, "values"=getPublicTotals(local.year)});
}
return {"labels" = "Y1,Y2,Y3,Y4,Y5,Y6", "records" = local.records };
}
// Old Function from OP
remote struct function getDataForJSVersionOld() returnformat="json" {
return {
"labels" = "Y1,Y2,Y3,Y4,Y5,Y6",
"records" = [
{
"text" ="Fall 2006",
"values"=[1,2,4,8,6,7]
},{
"text" ="Fall 2008",
"values"=[10,8,6,4,6,2]
},{
"text" ="Fall 2010",
"values"=[1,3,9,3,1,7]
}
]
};
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment