我尝试使用 ember-data 的“每种类型的适配器”功能。不确定我是否做错了什么,或者它是否是 ember-data 中的错误。基本上我认为它会像这样工作:
Person = DS.Model.extend({
name: DS.attr('string')
});
var personAdapter = DS.Adapter.create();
DS.Store.registerAdapter(Person, personAdapter );
商店总是使用默认适配器而不是注册的“每种类型的适配器”我写了一个测试用例来说明我的意思:
var get = Ember.get, set = Ember.set;
var Person, store, adapter, personAdapter;
module("DS.Store and DS.Adapter 'adapter per type' integration test", {
setup: function() {
Person = DS.Model.extend({
name: DS.attr('string')
});
adapter = DS.Adapter.create();
personAdapter = DS.Adapter.create();
DS.Store.registerAdapter(Person, personAdapter);
store = DS.Store.create({ adapter: adapter });
},
teardown: function() {
adapter.destroy();
store.destroy();
}
});
test("test function on the per type adapter", function() {
adapter.find = function(store, type, id) {
ok(false, "should call find on the registered 'per type adapter' not on the default one");
};
personAdapter.find = function(store, type, id) {
store.load(Person, {
'id': 1,
'name': "My Name"
});
};
var person = store.find(Person, 1);
equal(person.get('name'), "My Name");
});
在调试时,我注意到“DS.Mappable._reifyMapping”混合显式检查 DS.Store 并在“this”是 DS.Store 时停止。