George Eracleous
George Eracleous

Reputation: 4422

Add extra url params per 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 are both using the same Adapter. However, for the Products model I want to append an extra url param (the api key) to the url. How can I extend the adapter (or the serializer?) to implement this?

So just to give you an example when 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 occurs when I do:

var providers = App.Providers.find(1);
providers.get('products');

EDIT: I have tried to override the buildURL method in the adapter but it's not very convenient since I want to append the api_key param only for certain models.

Upvotes: 3

Views: 1620

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

You should create a second adapter which overrides the buildURL method. Then register that adapter for any types that should be using an api key.

apiAdapter = originalAdapter.extend({
  buildURL: ....
}));

Store.registerAdapter(App.Providers, apiAdatper);

See this post for more detail on per-type adapters: How to use DS.Store.registerAdapter

Upvotes: 4

Related Questions