$.Model.model is used as a Ajax converter to convert the response of a jQuery.Model.findOne request into a model instance.
You will never call this method directly. Instead, you tell $.ajax about it in findOne:
$.Model('Recipe',{ findOne : function(params, success, error ){ return $.ajax({ url: '/services/recipes/'+params.id+'.json', type: 'get', dataType : 'json recipe.model' //LOOK HERE! }); } },{})
This makes the result of findOne a $.Deferred that resolves to a model instance:
var deferredRecipe = Recipe.findOne({id: 6}); deferredRecipe.then(function(recipe){ console.log('I am '+recipes.description+'.'); })
$.jQuery.model expects data to be name-value pairs like:
{id: 1, name : "justin"}
It can also take an object with attributes in a data, attributes, or 'shortName' property. For a App.Models.Person model the following will all work:
{ data : {id: 1, name : "justin"} } { attributes : {id: 1, name : "justin"} } { person : {id: 1, name : "justin"} }
If your service returns data like:
{id : 1, name: "justin", data: {foo : "bar"} }
This will confuse $.Model.model. You will want to overwrite it to create an instance manually:
$.Model('Person',{ model : function(data){ return new this(data); } },{})
$.Model.model(attributes) -> Model
{Object}
An object of name-value pairs or an object that has a data, attributes, or 'shortName' property that maps to an object of name-value pairs.
{Model}
an instance of the model
$.Model.model is used as a Ajax converter to convert the response of a jQuery.Model.findOne request into a model instance.
You will never call this method directly. Instead, you tell $.ajax about it in findOne:
This makes the result of findOne a $.Deferred that resolves to a model instance:
Non-standard Services
$.jQuery.model expects data to be name-value pairs like:
It can also take an object with attributes in a data, attributes, or 'shortName' property. For a App.Models.Person model the following will all work:
Overwriting Model
If your service returns data like:
This will confuse $.Model.model. You will want to overwrite it to create an instance manually:
API