-
Notifications
You must be signed in to change notification settings - Fork 0
How To Guide
Chris Phillips edited this page Jan 24, 2019
·
1 revision
Data components must have accessors=true
.
To enable data components to contain properties that are arrays of other components, you must use item_type
on properties.
// /test_cfcs/Bundle.cfc
component accessors=true {
property name="id" type="numeric";
property name="name" type="string";
property name="widgets" type="array" item_type="test_cfcs.Widget";
}
// /test_cfcs/Widget.cfc
component accessors=true {
property name="id" type="numeric";
property name="inBundles" type="array" item_type="test_cfcs.Bundle";
property name="name" type="string";
property name="options" type="array" item_type="test_cfcs.Option";
}
// /test_cfcs/Widget.cfc
component accessors=true {
property name="id" type="numeric";
property name="name" type="string";
}
Note: While it is not required that you cache an instance of "loader.cfc", you should cache it. The loader caches instances of "component loaders", that in turn, cache "template data components" and pointers to "peer component loaders". So, a "warm" or "primed" instance of the loader will be a bit faster.
// Plain old CFML
var loader = new com.github.cfchris.cfc_loader.loader();
loader.setLoadersPath("com.generated.loaders"); // directory should exist (probably with .gitignore on *.cfc)
// ColdSpring
<bean id="VoLoader" class="com.github.cfchris.cfc_loader.loader">
<property name="LoadersPath"><value>com.generated.loaders</value></property>
</bean>
Once you have configured the loader, you just need to call load
. (e.g. loader.load(new foo(), {id: 1})
)
// Again. You should cache your loader.
// But, for this example, it is uncached.
var loader = new com.github.cfchris.cfc_loader.loader();
loader.setLoadersPath("com.generated.loaders");
// Often data come as JSON from APIs
var apiData = '{
"id": 1,
"name": "Benchmark Bundle",
"widgets": [
{
"id": 1,
"name": "Benchmark Widget 1",
"options": [
{"id": 1, "name": "Benchmark Option 1"},
{"id": 2, "name": "Benchmark Option 2"}
],
"isInBundles": [
{"id": 1, "Name": "Benchmark Bundle"},
{"id": 7, "Name": "Some Other Bundle"}
]
},
{
"id": 2,
"name": "Benchmark Widget 2",
"options": [
{"id": 3, "name": "Benchmark Option 3"},
{"id": 4, "name": "Benchmark Option 4"}
],
"isInBundles": [
{"id": 1, "Name": "Benchmark Bundle"},
{"id": 42, "Name": "Yet Another Bundle"}
]
}
]
}';
// Loading a CFC from an API response, can be as simple as this
return loader.load(new test_cfcs.Bundle(), DeserializeJson(apiData));
That's it. I hope cfc_loader makes working with ColdFusion components simpler and easier for you.