Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : Update several model instances at once

$
0
0
I have implemented it in the past as a method on can.Model.List.  Say you wanted to be able to update a list of recipes:

  1. Recipe = can.Model({
  2.   findAll: "/recipes"
  3. },{})


  4. Recipe.List = can.Model.List({
  5.   update: function( props ) {
  6.     if( props ){
  7.       this.each(function(item){ item.attr(props)   })
  8.     }
  9.     var data = this.serialize(),
  10.           self = this;
  11.     return $.ajax({
  12.       url: "/recipes",
  13.       type: "PUT",
  14.       data: data,
  15.       success: function(data){
  16.         // might be used to update the isntances data like:
  17.         // self[i].attr(data)
  18.       },
  19.       dataType: "json"
  20.     })
  21.   }
  22. })


Use it like:


  1. Recipe.findAll({}, function(recipes){
  2.   recipes.update({tasty: true}).then(function(){

  3.   })
  4. })


Viewing all articles
Browse latest Browse all 3491

Trending Articles