在适配器和Fixture适配器和REST适配器之间有什么区别,在ember数据? [英] What is the difference between Adapter and Fixture Adapter and REST Adapter, in ember-data?

查看:128
本文介绍了在适配器和Fixture适配器和REST适配器之间有什么区别,在ember数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

使用 DS.FixtureAdapter (或 DS.FixtureAdapter.create())当你不(关心)沟通带有后端,但会将您的数据作为灯具存储在客户端。一旦你宣布了一个模型:

  App.Thing = DS.Model.extend({
name:DS .attr('string'),
// ...
});

您可以定义您的灯具:

  App.Thing.FIXTURES = [
{
id:1,
name:'...',
// ...
},
{
id:2,
名称:'...',
// ...
},
]。

然后,您可以使用它们上的ember-data方法(例如 App .Thing.findAll()等)并操纵它们,但当然这只会保持页面一致(即JavaScript环境)。



DS.RestAdapter ,尽管显然还在开发中,可以使用Rails API,但可能会被修改/扩展以适应无论您正在使用的RESTful API。它通过调用 / something 来处理 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 相当抽象:上述内置适配器的超类。如果既不适合您的需要,则可能希望自己实现:

  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 上的自述文件。


What is the difference between Adapter and Fixture Adapter and REST Adapter, and when to use each one?

解决方案

Use DS.FixtureAdapter (or DS.FixtureAdapter.create()) when you don't (yet?) care to communicate with a backend, but will store your data as "fixtures" in the client. Once you've declared a model:

App.Thing = DS.Model.extend({
  name: DS.attr('string'),
  // ...
});

You can define your fixtures:

App.Thing.FIXTURES = [
  {
    id: 1,
    name: '...',
    // ...
  },
  {
    id: 2,
    name: '...',
    // ...
  },
];

And then you can use the ember-data methods on them (e.g. App.Thing.findAll(), etc.) and manipulate them, but of course it will only persist as long as the page does (i.e. the javascript environment).

DS.RestAdapter, usable though apparently still under development, was designed to fit well with a Rails API, but could probably be modified / extended to work with whatever RESTful API you're working with. It knows to process App.Thing.findAll() by making a call to /things, and to process App.Thing.find(12) with a call to /things/12. This is a relative path, appended to the namespace parameter you pass in:

App.store = DS.Store.create({
  revision: 4,
  adapter: DS.RestAdapter.create({
    namespace: 'http://what.ever/api/v1'
  })
});

DS.Adapter is rather abstract: the superclass of the aforementioned built-in Adapters. If neither suit your needs, you may well want to implement your own:

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
});

Hope that helps. See the readme doc at https://github.com/emberjs/data for more information.

这篇关于在适配器和Fixture适配器和REST适配器之间有什么区别,在ember数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆