2

我正在为 Ember 数据滚动我自己的适配器。长话短说,当我调用 App.store.updateRecord(App.Model, id ) 时,我收到此错误:

Uncaught TypeError: Object <DS.Store:ember195> has no method 'updateRecord'

即使实现了 updateRecord 功能。

下面的示例代码(为了清楚起见,我将所有函数都记录到控制台而不是做他们应该做的事情)

// declare application namespace
App = Ember.Application.create();

// instantiate store
App.store = DS.Store.create({
    revision: 2,
    adapter: DS.LocalStorageAdapter.create(),
});

// implement adapter
DS.LocalStorageAdapter = DS.Adapter.extend({

  createRecord: function(store, type, model) {
    console.log('createRecord: ', type, model);

  },


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

  },

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

  },

  localStorage: {
    set: function( ModelTyp, value ){},

    get: function( ModelType ){},
  }

});

// create model
App.StyleData = DS.Model.extend({

    css_name: DS.attr('string', {key: 'css_name'}),
    storageID: DS.attr('number', {defaultValue: 0, key: 'storageID'}),

});

// ==========================================================================
// Test Application
// ==========================================================================


App.store.createRecord(App.StyleData, { css_name: 'name' });
App.store.commit()   
//console: createRecord:  App.StyleData, model

App.store.find(App.StyleData, 0)   
//console: find: App.StyleData, 0

App.store.updateRecord(App.StyleData, { css_name: 'new name' });  
//console: Uncaught TypeError: Object <DS.Store:ember195> has no method 'updateRecord' 

因为我无法弄清楚引擎盖下发生了什么,所以我几乎无所适从。

4

1 回答 1

4

我曾一度处于相同的位置,因为商店的方法(createRecorddeleteRecordfind等)和适配器的方法之间的概念差异在可用文档中似乎并没有真正明确。

至于updateRecord,店里没有。要更新,您只需.setModeland run中的值App.store.commit()。这将调用适配器的updateRecord.

于 2012-09-20T19:25:56.063 回答