using backbone can fetch
collection below , render backbone view each record:
person = backbone.model.extend({}); var personcollection = backbone.collection.extend({ model: person, url: '/home/people/' });
when spin new person using backbone below, isn't supposed handle .save() functionality posting url defined in above collection?
var p = new person({ name: 'andrew', age: 24 }); p.save(); // uncaught error: 'url' property or function must specified // thought supposed use collection's url? // can around explicitly setting p.url doesn't seem right
looks approach use current collection , create
handles adding collection , saving it
this.people.create(p);
the source below. create off collection, sets models collection this
once save success adds model collection. passes model geturl, geturl looks @ url property on model, in turn passes collection geturl again... way don't have redefine url...
create : function(model, options) { var coll = this; options || (options = {}); if (!(model instanceof backbone.model)) { model = new this.model(model, {collection: coll}); } else { model.collection = coll; } var success = function(nextmodel, resp) { coll.add(nextmodel); if (options.success) options.success(nextmodel, resp); }; return model.save(null, {success : success, error : options.error}); },
Comments
Post a Comment