使用Ember.js为每个模型添加额外的url参数 [英] Add extra url params per model with Ember.js
问题描述
我有两个模型:
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'),
});
他们都使用相同的适配器。但是,对于产品模型,我想在url中附加一个额外的URL参数(api键)。如何扩展适配器(或串行器?)来实现?
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?
所以只是给我一个例子,当我想为提供者做一个GET:
So just to give you an example when I want to do a GET for providers:
http://example.com/ap1/v1/providers/
和对于产品:
http://example.com/ap1/v1/products/?api_key=1234
我知道我可以添加这个,当我做 App.Products.find({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');
编辑:
我试图覆盖适配器中的buildURL方法,但不是很方便,因为我只想为某些型号追加api_key参数。
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.
推荐答案
您应该创建一个覆盖buildURL方法的第二个适配器。然后将该适配器注册给应该使用api密钥的任何类型。
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);
有关每种类型适配器的详细信息,请参阅此文章:如何使用DS.Store.registerAdapter
See this post for more detail on per-type adapters: How to use DS.Store.registerAdapter
这篇关于使用Ember.js为每个模型添加额外的url参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!