0

在 Ember Data 中,在我声明一个模型并提交到商店后,我检索它并尝试为特定字段设置一个新值,但是它给了我这个错误:

Uncaught Error: <DS.StateManager:ember227> could not respond to event setProperty in state rootState.loaded.created.inFlight.
Ember.StateManager.Ember.State.extend.sendRecursively ember-latest.min.js:17
Ember.StateManager.Ember.State.extend.sendRecursively ember-latest.min.js:17
Ember.StateManager.Ember.State.extend.sendRecursively ember-latest.min.js:17
Ember.StateManager.Ember.State.extend.sendRecursively ember-latest.min.js:17
Ember.StateManager.Ember.State.extend.sendRecursively ember-latest.min.js:17
Ember.StateManager.Ember.State.extend.send ember-latest.min.js:17
DS.Model.Ember.Object.extend.send ember-data.js:2540
DS.Model.Ember.Object.extend.setProperty ember-data.js:2549
(anonymous function) ember-data.js:2682
n.set ember-latest.min.js:14
c ember-latest.min.js:14
Ember.Observable.Ember.Mixin.create.set ember-latest.min.js:15
(anonymous function)

详细信息(主要是样板代码):

/* create store */
 App.store = DS.Store.create({
    revision: 2,
    // adapter: DS.LocalStorageAdaptor.create({}),
    adapter: DS.DjangoTastypieAdapter.create({
      serverDomain:  "http://localhost:8000",
      tastypieApiUrl: 'api/v1/',
    });
});

// declare model
App.StyleData = DS.Model.extend({
    ...
    storageID: DS.attr('number', {defaultValue: 0, key: 'storageID'}),

    didCreate: function(){
        // console.log('didcreate');
    },

});

/*create new model */

App.store.createRecord(App.StyleData, {storageID: 0});
App.store.commit();

/* retrieve model and set new value for storageID */
var mod = App.store.findAll( App.StyleData );
var mod1  = mod.get('modelCache')[0];
mod1.set('storageID', 10)     // here the error above is thrown 
4

2 回答 2

3

此错误来自在通过 API 调用保存记录时尝试修改记录。在您的示例中,App.store.commit()启动 ajax 操作以将新记录的详细信息发送到您的 API。Ajax 调用是异步操作,因此记录会进入状态,直到 API 响应,如果一切成功inFlight,ember-data 会将记录转换回状态。clean虽然它是inFlight,但它可能不会被修改。

于 2012-09-15T12:20:32.057 回答
2

试试这个方法:

var update = function(){
  if(mod.isLoaded()){
    mod1.set('storageID', 10);
  }
}.observes('mod.isLoaded')
于 2012-09-15T12:22:46.860 回答