-
Notifications
You must be signed in to change notification settings - Fork 4
Functions in JSON
Andre Kless edited this page Oct 25, 2018
·
32 revisions
content is up-to-date for ccm v18.0.6
The JSON data format does not allow JavaScript functions. Therefore, we had to look for a workaround, to get functions included into JSON structures. ccm offers two main ways to include functions into JSON data structures:
- with placeholders, e.g.
%event%
- with ccm Action Data, e.g.
[ "ccm.load", "path" ]
This works with the helper functions ccm.helper.html and ccm.helper.format. Most common use of such placeholders is in ccm HTML Data for HTML templates. Regardless of HTML templates, placeholders can also be replaced by functions for any JSON.
const html = {
"tag": "button",
"id": "%id%",
"inner": "%caption%",
"onclick": "%click%"
}
const button = ccm.helper.html( html, {
id: 'feedback-btn',
caption: 'Feedback',
click: () => console.log( 'Thanks for Feedback!' )
} );
document.body.appendChild( button );
or
const html = {
"tag": "button",
"id": "%%",
"inner": "%%",
"onclick": "%%"
}
const button = ccm.helper.html( html, "feedback-btn", "Feedback", () => console.log( 'Thanks for Feedback!' ) );
document.body.appendChild( button );
const json = {
"id": "greeting",
"sayHello": "%fkt%"
};
const obj = ccm.helper.format( json, {
fkt: () => console.log( 'Hello, World!' )
} );
obj.sayHello();
or
const json = {
"id": "greeting",
"sayHello": "%%"
};
const obj = ccm.helper.format( json, () => console.log( 'Hello, World!' ) );
obj.sayHello();
See here.