I have a Clock
model in Backbone:
var Clock = Backbone.Model.extend({});
I'm trying to get an instance of that that has the latest information from /clocks/123
. Some things I've tried:
Clock.fetch(123) // TypeError: Object function (){ ... } has no method 'fetch'
fetch
on it:c = new Clock({id: 123}) c.fetch() // Error: A 'url' property or function must be specified
I tried creating an AllClocks
collection resource (even though I have no use for such a thing on the page):
var AllClocks = Backbone.Collection.extend({ model: Clock, url: '/clocks/' }); var allClocks = new AllClocks(); allClocks.fetch(123); // returns everything from /clocks/
How do I just get one API-backed Clock?
js Get model is used to get the value of an attribute on a model. Syntax: model. get(attribute)
Model. Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control. You extend Backbone.
Backbone is known for being lightweight, as its only hard dependency is on one JavaScript library, Underscore. js, plus jQuery for use of the full library. It is designed for developing single-page web applications, and for keeping various parts of web applications (e.g. multiple clients and the server) synchronized.
Try specifying urlRoot in the model:
From the docs:
var Book = Backbone.Model.extend({urlRoot : '/books'}); var solaris = new Book({id: "1083-lem-solaris"}); solaris.fetch();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With