Models
AJAX Abstraction for a Single Resource
AJAX Abstraction for a Single Resource
The url()
method is responsible for constructing the final URL used for API calls.
The default behavior is to append the model's id
to the urlRoot
. For example, if the urlRoot
is http://localhost:1337/tweets
, then the url()
will be one of the following:
id
, then url()
will return http://localhost:1337/tweets
id
of 1
, then url()
will return http://localhost:1337/tweets/1
The default implementation looks like this:
url: function() {
const base =
_.result(this, 'urlRoot') ||
_.result(this.collection, 'url') ||
urlError();
if (this.isNew()) {
return base;
}
const id = this.get(this.idAttribute);
return base.replace(/[^\/]$/, '$&/') + encodeURIComponent(id);
},
One use for url()
is to construct nested URLs. You can read more about that in the initialize()
documentation here.