Add extra url params per model with Ember.js

ぃ、小莉子 提交于 2019-12-09 19:00:43

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/14710296/add-extra-url-params-per-model-with-ember-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!