Models

AJAX Abstraction for a Single Resource

url

The url() method is responsible for constructing the final URL used for API calls.

Default Behavior

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:

  • If the model has no id, then url() will return http://localhost:1337/tweets
  • If the model has an id of 1, then url() will return http://localhost:1337/tweets/1

Default Implementation

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);
},

Usage

One use for url() is to construct nested URLs. You can read more about that in the initialize() documentation here.