Class DS.BuildURLMixin

Show:

Module: ember-data
modelName
String
id
(String|Array|Object)
single id or array of ids or query
snapshot
(DS.Snapshot|Array)
single snapshot or array of snapshots
requestType
String
query
Object
object of query parameters to send for query requests.
returns
String
url

Builds a URL for a given type and optional ID.

By default, it pluralizes the type's name (for example, 'post' becomes 'posts' and 'person' becomes 'people'). To override the pluralization see pathForType.

If an ID is specified, it adds the ID to the path generated for the type, separated by a /.

When called by RESTAdapter.findMany() the id and snapshot parameters will be arrays of ids and snapshots.

Module: ember-data
modelName
String
returns
String
path

Determines the pathname for a given type.

By default, it pluralizes the type's name (for example, 'post' becomes 'posts' and 'person' becomes 'people').

Pathname customization

For example, if you have an object LineItem with an endpoint of /line_items/.

app/adapters/application.js
1
2
3
4
5
6
7
8
9
10
import RESTAdapter from '@ember-data/adapter/rest';
import { decamelize } from '@ember/string';
import { pluralize } from 'ember-inflector';

export default RESTAdapter.extend({
  pathForType: function(modelName) {
    var decamelized = decamelize(modelName);
    return pluralize(decamelized);
  }
});
Module: ember-data
modelName
String
snapshot
DS.Snapshot
returns
String
url

Builds a URL for a record.save() call when the record was created locally using store.createRecord().

Example:

app/adapters/application.js
1
2
3
4
5
6
7
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
  urlForCreateRecord(modelName, snapshot) {
    return this._super(...arguments) + '/new';
  }
});
Module: ember-data
id
String
modelName
String
snapshot
DS.Snapshot
returns
String
url

Builds a URL for a record.save() call when the record has been deleted locally.

Example:

app/adapters/application.js
1
2
3
4
5
6
7
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
  urlForDeleteRecord(id, modelName, snapshot) {
    return this._super(...arguments) + '/destroy';
  }
});
Module: ember-data
modelName
String
snapshot
DS.SnapshotRecordArray
returns
String
url

Builds a URL for a store.findAll(type) call.

Example:

app/adapters/comment.js
1
2
3
4
5
6
7
import JSONAPIAdapter from '@ember-data/adapter/json-api';

export default JSONAPIAdapter.extend({
  urlForFindAll(modelName, snapshot) {
    return 'data/comments.json';
  }
});
Module: ember-data
id
String
modelName
String
snapshot
DS.Snapshot
returns
String
url

Builds a URL for fetching an async belongsTo relationship when a url is not provided by the server.

Example:

app/adapters/application.js
1
2
3
4
5
6
7
8
import JSONAPIAdapter from '@ember-data/adapter/json-api';

export default JSONAPIAdapter.extend({
  urlForFindBelongsTo(id, modelName, snapshot) {
    let baseUrl = this.buildURL(modelName, id);
    return `${baseUrl}/relationships`;
  }
});
Module: ember-data
id
String
modelName
String
snapshot
DS.Snapshot
returns
String
url

Builds a URL for fetching an async hasMany relationship when a URL is not provided by the server.

Example:

app/adapters/application.js
1
2
3
4
5
6
7
8
import JSONAPIAdapter from '@ember-data/adapter/json-api';

export default JSONAPIAdapter.extend({
  urlForFindHasMany(id, modelName, snapshot) {
    let baseUrl = this.buildURL(modelName, id);
    return `${baseUrl}/relationships`;
  }
});
Module: ember-data
ids
Array
modelName
String
snapshots
Array
returns
String
url

Builds a URL for coalescing multiple store.findRecord(type, id) records into 1 request when the adapter's coalesceFindRequests property is true.

Example:

app/adapters/application.js
1
2
3
4
5
6
7
8
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
  urlForFindMany(ids, modelName) {
    let baseUrl = this.buildURL();
    return `${baseUrl}/coalesce`;
  }
});
Module: ember-data
id
String
modelName
String
snapshot
DS.Snapshot
returns
String
url

Builds a URL for a store.findRecord(type, id) call.

Example:

app/adapters/user.js
1
2
3
4
5
6
7
8
import JSONAPIAdapter from '@ember-data/adapter/json-api';

export default JSONAPIAdapter.extend({
  urlForFindRecord(id, modelName, snapshot) {
    let baseUrl = this.buildURL(modelName, id, snapshot);
    return `${baseUrl}/users/${snapshot.adapterOptions.user_id}/playlists/${id}`;
  }
});
Module: ember-data
query
Object
modelName
String
returns
String
url

Builds a URL for a store.query(type, query) call.

Example:

app/adapters/application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
  host: 'https://api.github.com',
  urlForQuery (query, modelName) {
    switch(modelName) {
      case 'repo':
        return `https://api.github.com/orgs/${query.orgId}/repos`;
      default:
        return this._super(...arguments);
    }
  }
});
Module: ember-data
query
Object
modelName
String
returns
String
url

Builds a URL for a store.queryRecord(type, query) call.

Example:

app/adapters/application.js
1
2
3
4
5
6
7
8
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
  urlForQueryRecord({ slug }, modelName) {
    let baseUrl = this.buildURL();
    return `${baseUrl}/${encodeURIComponent(slug)}`;
  }
});
Module: ember-data
id
String
modelName
String
snapshot
DS.Snapshot
returns
String
url

Builds a URL for a record.save() call when the record has been updated locally.

Example:

app/adapters/application.js
1
2
3
4
5
6
7
import RESTAdapter from '@ember-data/adapter/rest';

export default RESTAdapter.extend({
  urlForUpdateRecord(id, modelName, snapshot) {
    return `/${id}/feed?access_token=${snapshot.adapterOptions.token}`;
  }
});