jQuery.Model.static.model  function     

$.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+'.');
})

Non-standard Services

$.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"} }

Overwriting Model

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);
  }
},{})

API

$.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

© Jupiter IT - JavaScriptMVC Training and Support