Add additional url parameters for each model with Ember.js
I have two models:
App.Providers = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string'),
logo: DS.attr('string'),
products: DS.hasMany('App.Products')
});
App.Products = DS.Model.extend({
name: DS.attr('string'),
description: DS.attr('string')
provider: DS.belongsTo('App.Providers'),
});
They both use the same adapter. However, for the Products model, I want to add an additional url (api key) to the url. How can I extend the adapter (or serializer?) To implement it?
So, just to give you an example where I want to do a GET for providers:
http://example.com/ap1/v1/providers/
and for products:
http://example.com/ap1/v1/products/?api_key=1234
I know I can add this when I do App.Products.find({api_key=1234})
, but the problem comes up when I do:
var providers = App.Providers.find(1);
providers.get('products');
EDIT: I tried to override the buildURL method in the adapter, but it's not very convenient as I only want to add the api_key parameter for certain models.
source to share
You have to create a second adapter that overrides the buildURL method. Then register this adapter for any types that need to use the api key.
apiAdapter = originalAdapter.extend({ buildURL: .... })); Store.registerAdapter(App.Providers, apiAdatper);
See this post for each type of adapters for details: How to use DS.Store.registerAdapter
source to share