$.Model.models is used as a Ajax converter to convert the response of a jQuery.Model.findAll request into an array (or $.Model.List) of model instances.
You will never call this method directly. Instead, you tell $.ajax about it in findAll:
$.Model('Recipe',{ findAll : function(params, success, error ){ return $.ajax({ url: '/services/recipes.json', type: 'get', data: params dataType : 'json recipe.models' //LOOK HERE! }); } },{})
This makes the result of findAll a $.Deferred that resolves to a list of model instances:
var deferredRecipes = Recipe.findAll({}); deferredRecipes.then(function(recipes){ console.log('I have '+recipes.length+'recipes.'); })
$.jQuery.models expects data to be an array of name-value pairs like:
[{id: 1, name : "justin"},{id:2, name: "brian"}, ...]
It can also take an object with additional data about the array like:
{ count: 15000 //how many total items there might be data: [{id: 1, name : "justin"},{id:2, name: "brian"}, ...] }
In this case, models will return an array of instances found in data, but with additional properties as expandos on the array:
var people = Person.models({ count : 1500, data : [{id: 1, name: 'justin'}, ...] }) people[0].name // -> justin people.count // -> 1500
If your service returns data like:
{ballers: [{name: "justin", id: 5}]}
You will want to overwrite models to pass the base models what it expects like:
$.Model('Person',{ models : function(data){ this._super(data.ballers); } },{})
$.Model.models(instancesRawData) -> Array
{Array}
an array of raw name - value pairs.
a JavaScript array of instances or a list of instances if the model list plugin has been included.
$.Model.models is used as a Ajax converter to convert the response of a jQuery.Model.findAll request into an array (or $.Model.List) of model instances.
You will never call this method directly. Instead, you tell $.ajax about it in findAll:
This makes the result of findAll a $.Deferred that resolves to a list of model instances:
Non-standard Services
$.jQuery.models expects data to be an array of name-value pairs like:
It can also take an object with additional data about the array like:
In this case, models will return an array of instances found in data, but with additional properties as expandos on the array:
Overwriting Models
If your service returns data like:
You will want to overwrite models to pass the base models what it expects like: