【问题标题】:Custom adapter for Ember Data does not workEmber 数据的自定义适配器不起作用
【发布时间】:2014-06-03 06:49:19
【问题描述】:

我使用本指南为 Ember Data 创建自定义适配器:http://emberjs.com/api/data/classes/DS.Adapter.html

在我的 app.js 中,我有以下内容:

App.store = DS.Store.create({
    adapter: 'MyAdapter'
});

我创建了适配器:

App.MyAdapter = DS.Adapter.extend({
    createRecord: function(store, type, record) {
        console.log('create');
    },

    deleteRecord: function(store, type, record) {
        console.log('delete');
    },

    find: function(store, type, id) {
        console.log('find');
    },

    findAll: function(store, type, sinceToken) {
        console.log('findAll');
    },

    findQuery: function(store, type, query) {
        console.log('findQuery');
    },

    updateRecord: function(store, type, record) {
        console.log('updateRecord');
    }
});

我创建了一个模型:

App.Post = DS.Model.extend({
    title: DS.attr,
    body: DS.attr,
    author: DS.attr
});

然后在我的路线中:

this.store.createRecord('post', {
    title: 'Rails is Omakase',
    body: 'Lorem ipsum',
    author: 'asd'
});

var post = this.store.find('post');
console.log(post);

很遗憾,这不起作用,因为控制台输出中有几个错误:

  • Assertion failed: Unable to find fixtures for model type App.Post
  • Assertion failed: The response from a findAll must be an Array, not null
  • TypeError: Cannot read property 'map' of null

所以我的第一个猜测是,它找不到我的自定义适配器,因为如果调用该函数(console.log('find')...)也没有日志记录。文档甚至是最新的吗?字符串MyAdapter 是否正确?不应该是这样吗:

App.store = DS.Store.create({
    adapter: 'App.MyAdapter'
});

没有任何作用。我做错了什么?

还有一个问题:有没有适合 web 套接字的 ember 数据适配器?

【问题讨论】:

    标签: ember.js ember-data adapter


    【解决方案1】:

    此文档指向 ember-data 1.0.0-beta.6 版本,似乎不正确。与此同时,fix was sent,但它只在 master 分支上可用。

    您可以按照新文档更新您的代码以使用App.ApplicationAdapter

    var MyAdapter = DS.Adapter.extend({
      ...
    });
    
    App.ApplicationAdapter = MyAdapter;
    

    当然你会收到错误,因为空方法,但 findAll 会被记录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多