jdat
A wrapper for an array of models with a JQuery like syntax
installation
$ npm install jdat
usage
jdat will add methods to an object prototype:
var jdat = ; var { thismodels = arr;} HeroClassprototype{ return thismodels0speed/2;} var herodata = name:'Superman' speed:9 children:... name:'Spiderman' speed:8 children:... name:'SilverSurfer' speed:11 children:... var heroes = herodata; var speeds = heroes var silver = heroes; silver heroes
api
jdat(options)
Assign the jdat methods to a prototype - the options are:
- proto - the prototype we will extend
- models - the field of the prototype that contains an array of models
- childfield - the field of each model that is a child array of models
- spawn - a function to return a new instance from an array of models
children()
Return an instance containing all children of the models in the array:
testdata // Superman
descendents()
Return an instance containing all descendents of the models in the array:
testdata // Superman & children names// Spiderman & children names// SilverSurfer & children names
recurse(fn)
Run a function over each of the descendents turned into a new instance:
testdata // Superman & children names// Spiderman & children names// SilverSurfer & children names
each(fn)
Loop over each model in the array and pass a spawned instance to a function:
testdata // Superman// Spiderman// SilverSurfer
map(fn)
Loop over each model in the array and pass a spawned instance to a function and return the mapped results:
var names = testdata // SUPERMAN// SPIDERMAN// SILVERSURFER
eq(index)
Return an instance of the class with the model from 'index' as the only model in the array:
var secondone = testdata; console; // Spiderman
first()
Return an instance with only the first model in its collection:
var first = testdata; console; // Superman
last()
Return an instance with only the last model in its collection:
var first = testdata; console; // SilverSurfer
get(index)
Return the raw model in the model array at 'index':
var secondmodel = testdata; console; // Spiderman
count()
Return the length of the model array
var count = testdata; console; // 3
clone()
Return an instance that has a copy (JSON.stringify / JSON.parse) of the models:
var newone = testdata;
spawn()
Use the factory function to return new instances of the class with different data:
var newone = testdata
instances()
Return an array of instances each with only one model in their array:
var instances = testdata; console; // 3
license
MIT