javascript - How do I fetch a single model in Backbone? -
i have clock model in backbone:
var clock = backbone.model.extend({}); i'm trying instance of that has latest information /clocks/123. things i've tried:
a "class"-level method
clock.fetch(123) // typeerror: object function (){ ... } has no method 'fetch' creating instance , calling fetch on it:
c = new clock({id: 123}) c.fetch() // error: 'url' property or function must specified a collection
i tried creating allclocks collection resource (even though have no use such thing on page):
var allclocks = backbone.collection.extend({ model: clock, url: '/clocks/' }); var allclocks = new allclocks(); allclocks.fetch(123); // returns /clocks/ how one api-backed clock?
your second approach approach have used. try adding following clock model:
url : function() { var base = 'clocks'; if (this.isnew()) return base; return base + (base.charat(base.length - 1) == '/' ? '' : '/') + this.id; }, this approach assumes have implemented controllers hashbang in url so, http://www.mydomain.com/#clocks/123 , should work if haven't yet.
Comments
Post a Comment