DS.BuildURLMixin Class
WARNING: This interface is likely to change in order to accomodate https://github.com/emberjs/rfcs/pull/4
Using BuildURLMixin
To use url building, include the mixin when extending an adapter, and call buildURL where needed.
The default behaviour is designed for RESTAdapter.
Example
export default DS.Adapter.extend(BuildURLMixin, {
findRecord: function(store, type, id, snapshot) {
var url = this.buildURL(type.modelName, id, snapshot, 'findRecord');
return this.ajax(url, 'GET');
}
});
Attributes
The host and namespace attributes will be used if defined, and are optional.
Item Index
Methods
_buildURL
-
modelName -
id
Parameters:
-
modelNameString -
idString
Returns:
url
buildURL
-
modelName -
id -
snapshot -
requestType -
query
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.
Parameters:
-
modelNameString -
id(String | Array | Object)single id or array of ids or query
-
snapshot(DS.Snapshot | Array)single snapshot or array of snapshots
-
requestTypeString -
queryObjectobject of query parameters to send for query requests.
Returns:
url
pathForType
-
modelName
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/".
import DS from 'ember-data';
import { decamelize } from '@ember/string';
import { pluralize } from 'ember-inflector';
export default DS.RESTAdapter.extend({
pathForType: function(modelName) {
var decamelized = decamelize(modelName);
return pluralize(decamelized);
}
});
Parameters:
-
modelNameString
Returns:
path
urlForCreateRecord
-
modelName -
snapshot
Builds a URL for a record.save() call when the record was created
locally using store.createRecord().
Example:
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
urlForCreateRecord(modelName, snapshot) {
return this._super(...arguments) + '/new';
}
});
Parameters:
-
modelNameString -
snapshotDS.Snapshot
Returns:
url
urlForDeleteRecord
-
id -
modelName -
snapshot
Builds a URL for a record.save() call when the record has been deleted locally.
Example:
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
urlForDeleteRecord(id, modelName, snapshot) {
return this._super(...arguments) + '/destroy';
}
});
Parameters:
-
idString -
modelNameString -
snapshotDS.Snapshot
Returns:
url
urlForFindAll
-
modelName -
snapshot
Builds a URL for a store.findAll(type) call.
Example:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
urlForFindAll(modelName, snapshot) {
return 'data/comments.json';
}
});
Parameters:
-
modelNameString -
snapshotDS.SnapshotRecordArray
Returns:
url
urlForFindBelongsTo
-
id -
modelName -
snapshot
Builds a URL for fetching a async belongsTo relationship when a url is not provided by the server.
Example:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
urlForFindBelongsTo(id, modelName, snapshot) {
let baseUrl = this.buildURL(id, modelName);
return ${baseUrl}/relationships;
}
});
Parameters:
-
idString -
modelNameString -
snapshotDS.Snapshot
Returns:
url
urlForFindHasMany
-
id -
modelName -
snapshot
Builds a URL for fetching a async hasMany relationship when a url is not provided by the server.
Example:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
urlForFindHasMany(id, modelName, snapshot) {
let baseUrl = this.buildURL(id, modelName);
return ${baseUrl}/relationships;
}
});
Parameters:
-
idString -
modelNameString -
snapshotDS.Snapshot
Returns:
url
urlForFindMany
-
ids -
modelName -
snapshots
Builds a URL for coalesceing multiple store.findRecord(type, id)
records into 1 request when the adapter's coalesceFindRequests
property is true.
Example:
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
urlForFindMany(ids, modelName) {
let baseUrl = this.buildURL();
return ${baseUrl}/coalesce;
}
});
Parameters:
-
idsArray -
modelNameString -
snapshotsArray
Returns:
url
urlForFindRecord
-
id -
modelName -
snapshot
Builds a URL for a store.findRecord(type, id) call.
Example:
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
urlForFindRecord(id, modelName, snapshot) {
let baseUrl = this.buildURL(modelName, id, snapshot);
return ${baseUrl}/users/${snapshot.adapterOptions.user_id}/playlists/${id};
}
});
Parameters:
-
idString -
modelNameString -
snapshotDS.Snapshot
Returns:
url
urlForQuery
-
query -
modelName
Builds a URL for a store.query(type, query) call.
Example:
import DS from 'ember-data';
export default DS.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);
}
}
});
Parameters:
-
queryObject -
modelNameString
Returns:
url
urlForQueryRecord
-
query -
modelName
Builds a URL for a store.queryRecord(type, query) call.
Example:
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
urlForQueryRecord({ slug }, modelName) {
let baseUrl = this.buildURL();
return ${baseUrl}/${encodeURIComponent(slug)};
}
});
Parameters:
-
queryObject -
modelNameString
Returns:
url
urlForUpdateRecord
-
id -
modelName -
snapshot
Builds a URL for a record.save() call when the record has been update locally.
Example:
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
urlForUpdateRecord(id, modelName, snapshot) {
return /${id}/feed?access_token=${snapshot.adapterOptions.token};
}
});
Parameters:
-
idString -
modelNameString -
snapshotDS.Snapshot
Returns:
url
urlPrefix
-
path -
parentURL
Parameters:
-
pathString -
parentURLString
Returns:
urlPrefix
