m is a subclass of Backbone.Model. I would expect m.url() to return '/api/media/sources/1' but instead I get:
> m.id
1
> m.collection.url
"/api/media/sources"
> m.urlRoot
undefined
> m.url()
"/api/media/sources"
What's going on?
m is a subclass of Backbone.Model. I would expect m.url() to return '/api/media/sources/1' but instead I get:
> m.id
1
> m.collection.url
"/api/media/sources"
> m.urlRoot
undefined
> m.url()
"/api/media/sources"
What's going on?
When you want the id
to be appended to the URL, you should set urlRoot
, not url
.
Take a look at the default implementation for Backbone.Model.prototype.url
and it will make sense:
url: function() {
var base =
_.result(this, 'urlRoot') ||
_.result(this.collection, 'url') ||
urlError();
if (this.isNew()) return base;
return base.replace(/([^\/])$/, '$1/') + encodeURIComponent(this.id);
},
Later comments revealed (after this answer was posted) that the problem had to do with how the attributes were being set.
In general, you should always use model.set("id", idValue)
for all attribute changes, even and especially the ID attribute. (If you have set idAttribute
to something else on the model prototype, use that name as the first argument instead of "id"
.)
More generally still, it's a bad idea to access model.attributes
directly- use model.set()
and model.get()
instead. The model.id
property is a convenience that you should basically treat as read-only; Backbone will modify it appropriately in every set()
call that involves the id attribute. – Platinum Azure 18 hours ago