jQuery.Model.static.wrapMany  function     

tags: deprecated

warning : wrapMany is deprecated in favor of jQuery.Model.models. They provide the same functionality; however, models works better with Deferreds.

$.Model.wrapMany converts a raw array of JavaScript Objects into an array (or $.Model.List) of model instances.

// a Recipe Model wi
$.Model("Recipe",{
  squareId : function(){
    return this.id*this.id;
  }
})

var recipes = Recipe.wrapMany([{id: 1},{id: 2}])
recipes[0].squareId() //-> 1

If an array is not passed to wrapMany, it will look in the object's .data property.

For example:

var recipes = Recipe.wrapMany({data: [{id: 1},{id: 2}]})
recipes[0].squareId() //-> 1

Often wrapMany is used with this.callback inside a model's findAll method like:

findAll : function(params, success, error){
  $.get('/url',
        params,
        this.callback(['wrapMany',success]), 'json' )
}

If you are having problems getting your model to callback success correctly, make sure a request is being made (with firebug's net tab). Also, you might not use this.callback and instead do:

findAll : function(params, success, error){
  self = this;
  $.get('/url',
        params,
        function(data){
          var wrapped = self.wrapMany(data);
          success(wrapped)
        },
        'json')
}

API

$.Model.wrapMany(instancesRawData) -> Array
{Array}

an array of raw name - value pairs like

[{name: "foo", id: 4},{name: "bar", id: 5}]
{Array}

a JavaScript array of instances or a list of instances if the model list plugin has been included.

© Jupiter IT - JavaScriptMVC Training and Support