-
Notifications
You must be signed in to change notification settings - Fork 22
Custom urlSegments
Template values are looked up by calling functions from urlSegments
in the adapter. ember-data-url-templates provides a number of default urlSegments
.
host
, namespace
, and pathForType
are delegated from the adapter.
id
, and query
are retrieved from buildURL
parameters.
All other values come from query
or snapshot
, trying query
first.
If a template value is needed that does not map directly to the default urlSegments, they can be configured in the urlSegments
attribute. A property of urlSegments
may be a computed property, or a function.
Functions will receive the following arguments: type, id, snapshot, query
, and will be called in the context of your adapter (i.e. this
will be whatever you are mixing UrlTemplates
into).
For example:
import Ember from 'ember';
import DS from 'ember-data';
import ENV from 'app/config/environment';
import UrlTemplates from 'ember-data-url-templates';
export default DS.RESTAdapter.extend(UrlTemplates, {
urlTemplate: '{+host}/api/{apiVersion}/{pathForType}{/id}{?category}',
urlSegments: {
category: function(type, id, snapshot, query) {
if (query && query.featured) { return "featured"; }
},
apiVersion: Ember.computed(function() {
return "v" + ENV.API_VERSION;
}),
},
});
In order to reference relationship ids, you need to use the snapshots api.
import DS from 'ember-data';
import UrlTemplates from 'ember-data-url-templates';
export default DS.RESTAdapter.extend(UrlTemplates, {
createRecordUrlTemplate: '{+host}/posts/{postId}/comments',
urlSegments: {
postId: function(type, id, snapshot, query) {
return snapshot.belongsTo('post', { id: true });
},
},
});