JavascriptObjectTemplates
Templates for Javascript Objects
How to use?
Create a template using a layout object describing the type and default value.
If the default value is null, then the field can be of any type.
var JOT = require('JavascriptObjectTemplates');
var template = new JOT({
myString: 'myStringValue',
myNumber: 1234
multipleValues: null,
subObject: {
subNumber: 5678
}
});
Note: This will be foundation for all following examples.
get(selector)
Get a value from the template.
template.get('myString');
template.get('myNumber');
template.get('subObject.subNumber');
set(selector, value)
Set a value to the template.
Note: Only accepts same type unless it's default value was null.
template.set('myString', 'myNewStringValue');
template.get('myString');
template.set('myString', 1234);
template.get('myString');
template.set('myBoolean', true);
merge(object)
Set multiple values from an object.
template.get('myString');
template.get('myNumber');
template.merge({
myString: 'myObjectStringValue',
myNumber: 'example',
myBoolean: false,
subObject: {
subNumber: 0
}
});
template.get('myString');
template.get('myNumber');
template.get('myBoolean');
template.get('subObject.subNumber');
getObject()
Get the template as object.
Can be used together with reset()
to allow the template to be used multiple times.
template.set('myString', 'myNewStringValue');
template.set('myNumber', 5678);
template.getObject();
reset()
Resets the template back to default values.
Can be used together with getObject()
to allow the template to be used multiple times.
template.set('myString', 'myNewStringValue');
template.set('myNumber', 5678);
template.getObject();
template.reset();
template.getObject();