Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember data - change model URL

I am using ember 2.0 and ember-data 2.0, and I have been struggling to find a way to pass custom URL to model.

For example, if my model is named Person and stored in model/person.js file, I would like rest web service url for finding record to be xxx/user/1, or in other words to avoid convention, and pass my URL to rest service - is is possible at all?

like image 347
Stefan Kostic Avatar asked Feb 11 '26 10:02

Stefan Kostic


1 Answers

You can use Adapter.

If your backend conventions differ from Ember Data convention, it easy to change its functionality by swapping out or extending the default Adapter.

App.ApplicationAdapter = DS.RESTAdapter.extend({
  namespace: 'api/v1',
  pathForType: function(type) {
    return Ember.Inflector.inflector.singularize(type);
  }
});

If you want just override a specific model just write new adapter with modelName + Adapter.
When I want to use a custom adapter for a 'note' model I can do something like:

App.Note = DS.Model.extend({
   title: DS.attr('string'),
   /* others attrs */
});

App.NoteAdapter = DS.RESTAdapter.extend({
  namespace: 'other/endpoint',
  pathForType: function(type) {
   return Ember.Inflector.inflector.pluralize(type);
  }
});

Take a look at ember adapter guide, if you use ember-cli use blueprint generator like:

ember generate adapter user

like image 139
Thibault Remy Avatar answered Feb 17 '26 06:02

Thibault Remy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!