Takes a model and updates its values using DOM data.
Install with component(1):
$ component install mikanda/sync-model
var syncModel = require('sync-model');
var model = require('model');
var el,
entity,
EntityClass;
el = document.querySelector('#my-form');
EntityClass = model(...)
.use(
syncModel({
date: function(value) {
return new Date(value);
}
})
)
.attr('my-attr', { type: 'string', format: 'date' });
entity = new EntityClass(...);
entity.syncWith(el);
Initialize the plugin. handlers
is an optional array with format
handlers. Format handlers are functions which are called to convert
values of a given format to their proper representation. They
should have the following definition:
function myHandler(value) {
// convert value to a representation
return convert(value);
}
The object looks like this:
{
'<format-name>': function(value) {},
...
}
Synchronizes the model with the data in the DOMElement
el
.
MIT