适配器、夹具适配器和 REST 适配器之间有什么区别,以及何时使用它们?
最佳答案
当您(还?)不关心与后端通信但会存储数据时,请使用 DS.FixtureAdapter(或 DS.FixtureAdapter.create())您的数据作为客户端中的“固定装置”。声明模型后:
App.Thing = DS.Model.extend({
name: DS.attr('string'),
// ...
});
您可以定义您的装置:
App.Thing.FIXTURES = [
{
id: 1,
name: '...',
// ...
},
{
id: 2,
name: '...',
// ...
},
];
然后您可以对它们使用 ember-data 方法(例如 App.Thing.findAll()
等)并操作它们,但当然它只会持续存在页面确实如此(即 javascript 环境)。
DS.RestAdapter
虽然显然仍在开发中,但仍可用,其设计目的是与 Rails API 很好地配合,但可能会被修改/扩展以与您正在使用的任何 RESTful API 一起使用。它知道通过调用 /things
来处理 App.Thing.findAll()
,并处理 App.Thing.find(12)
> 调用 /things/12
。这是一个相对路径,附加到您传入的命名空间参数中:
App.store = DS.Store.create({
revision: 4,
adapter: DS.RestAdapter.create({
namespace: 'http://what.ever/api/v1'
})
});
DS.Adapter
相当抽象:上述内置适配器的父类(super class)。如果两者都不满足您的需求,您可能想实现自己的:
App.adapter = DS.Adapter.create({
find: function(store, type, id) {
// ...
jQuery.get( ... , function(data) {
store.load(type, id, data);
});
},
createRecord: function(store, type, model) {
// ...
jQuery.post( ... , function(data) {
store.didCreateRecord(model, data);
});
},
// ...
});
App.store = DS.Store.create({
revision: 4,
adapter: App.adapter
});
希望有帮助。请参阅自述文件 https://github.com/emberjs/data了解更多信息。
关于ember.js - ember-data 中的适配器、夹具适配器和 REST 适配器之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11910496/