The answer depends on what you want to do with the server-side collection.
If you need to send additional data with a message, you may need a wrapper model or a relational model .
When using a wrapper model, you always need to write your own parse method:
var Occupants = Backbone.Collection.extend({ model: Person }); var House = Backbone.Model.extend({ url: function (){ return "/house/"+this.id; }, parse: function(response){ response.occupants = new Occupants(response.occupants) return response; } });
Relational models are better, I think, because you can easily customize them and you can adjust using the includeInJSON parameter, which attributes are placed in json, which you send to the recreation service.
var House = Backbone.RelationalModel.extend({ url: function (){ return "/house/"+this.id; }, relations: [ { type: Backbone.HasMany, key: 'occupants', relatedModel: Person, includeInJSON: ["id"], reverseRelation: { key: 'livesIn' } } ] });
If you do not send additional data , you can synchronize your own collection . You must add the save method to your collection (or prototype collection) in this case:
var Occupants = Backbone.Collection.extend({ url: "/concrete-house/occupants", model: Person, save: function (options) { this.sync("update", this, options); } });
inf3rno May 31 '13 at 15:44 2013-05-31 15:44
source share