A simple JS data model that emits events on data change. Offers support for collections of models.
Install the node module:
npm install trndymodel --save
trndyModel supports models and collections, this first example is for models only.
var Model = require('trndymodel').Model;
Create a new empty model:
var modelColor = new Model();
Optionally you can also set the model data at instantiation:
var modelColor = new Model({
name: 'red'
});
Add change listener to the model:
modelColor.on('change', function(data) {
console.log('Model Data Change:', data);
});
Whenever a model changes it will emit a 'change' event.
Model and collection events are emited using Node.js' events module. For more options on how to listen to events please look at the Node.js documentation:
https://nodejs.org/api/events.html
Save some data in the model:
modelColor.set({
name: 'red'
});
This will emit 'change' and 'set' events.
Retreive the stored model data:
var redColorData = modelColor.get();
Update the stored model data with new data:
modelColor.update({
name: 'blue',
isPrimaryColor: true
});
This extends the existing model data, old properties are overwritten, new properties are added to the model. This will emit 'change' and 'update' events.
This sets the model data to an empty object:
modelColor.delete();
This will emit 'change' and 'delete' events.
Collections are arrays. They can hold models or any other data types.
var Model = require('trndymodel').Model;
var Collection = require('trndymodel').Collection;
Create a couple new models.
var modelColor1 = new Model({
name: 'red'
});
var modelColor2 = new Model({
name: 'green'
});
var modelColor3 = new Model({
name: 'blue'
});
Now create a new collection to hold the models.
var modelColors = new Collection();
Optionally you can also set the collection data at instantiation:
var modelColors = new Collection([
modelColor1,
modelColor2,
modelColor3
]);
Add change listeners to the models and the collection:
models:
modelColor1.on('change', function(data) {
console.log('Model 1 Data Change:', data);
});
modelColor2.on('change', function(data) {
console.log('Model 2 Data Change:', data);
});
modelColor3.on('change', function(data) {
console.log('Model 3 Data Change:', data);
});
collection:
modelColors.on('change', function(data) {
console.log('Collection Data Change:', data);
});
Whenever these models or this collection change they will emit a 'change' event.
Model and collection events are emited using Node.js' events module. For more options on how to listen to events please look at the Node.js documentation:
https://nodejs.org/api/events.html
Set the collection array contents:
modelColor1.set([
modelColor1,
modelColor2,
modelColor3
]);
The data must be in an array and this will overwrite any existing array data stored completely. This will emit 'change' and 'set' events.
Add single items to the end of the stored collection array:
modelColors.push(modelColor1);
modelColors.push(modelColor2);
modelColors.push(modelColor3);
or add multiple items to the end of the stored collection array:
modelColor1.push([
modelColor1,
modelColor2,
modelColor3
]);
This will emit 'change' and 'push' events.
This returns then entire collection data array:
var allColors = modelColors.get();
This returns only a single item from the collection array at the specified index:
var greenData = modelColors.get(1);
This updates a single item in the collection array with new object data at the specified index:
modelColors.update(1, {
isPrimaryColor: true
});
This extends the existing item object data, old properties are overwritten, new properties are added to the object.
or when the item data type is not an object it will simply overwrite completely the old data with new:
modelColors.update(1, true);
This will emit 'change' and 'update' events on the collection and on the item if it is a model.
When you don't pass in an index argument the collection is updated with all new data:
modelColors.update([
new Model({
name: 'cyan'
}),
new Model({
name: 'magenta'
}),
new Model({
name: 'yellow'
}),
new Model({
name: 'black'
})
]);
The new data must be an array. This will emit 'change' and 'update' events only on the collection.
This deletes a model from the collection at the specified index:
modelColors.delete(2);
This will emit 'change' and 'delete' events.
This sets the collection data to an empty array.
modelColors.delete();
This will emit 'change' and 'delete' events.