我有以下设置:
App.Router.map(function() {
this.route('tab', { 'path' : 'tab/:which' });
});
App.ApplicationStore = DS.Store.extend({});
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: '../api'
});
App.TabAdapter = DS.RESTAdapter.extend({
find: function(store, type, id) {
alert("I doesn't get invoked");
return this._super(store, type, id);
}
});
App.TabRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('emails', {tab: "inbox"});
}
});
当访问路由#/tab/inbox
时,我想重写来自http://localhost/ba/api/emails?tab=inbox
进入http://localhost/ba/api/emails/inbox
。因此,我在TabAdapter上重写find()
-方法,但当this.store.find('emails', {tab: "inbox"});
运行时,它不会进入我重写的方法(我的测试警报也不会被调用)。
为什么我重写的find()
-方法没有被调用?
您覆盖了错误的find
方法。您是通过查询而不是id查找的,并且应该覆盖该方法
findQuery: function(store, type, query) {
// Do your thing here
return this.ajax(this.buildURL(type.typeKey), 'GET', { data: query });
}
您使用的是TabAdapter
,它特定于tab
类型的模型,而不是email(s)
类型的模型。您应该创建一个Email(s)Adapter
。一般的惯例是一个模型是奇异的
另请参阅:如何为ember.js创建自定义适配器?