【发布时间】:2013-04-29 22:45:11
【问题描述】:
我在使用 Ember-Data 交易时遇到问题。
我有一个这样的 DS.Model
App.MyModel = DS.Model.extend({
id: DS.attr(),
answers: DS.hasMany('App.Answer') // another model
});
然后它稍后会像这样在a Route中启动
model: function(){
var transaction = this.get('store').transaction();
return transaction.createRecord(App.MyModel, {
id: '1'
});
}
我有一个模型,它使用事务和提交向我的后端服务器发出请求。
this.get('content.transaction').commit();
目的是在服务器端更新答案并将其发回给我。 如果内容还没有更新,我称之为
this.get('content').reload();
然后再次发送请求。
这一切都很好。如果找到了 id,就会填充答案。
我的问题是,有时,根据我从服务器返回的内容,我必须发出另一个服务器请求。初始请求适用于
this.get('content.transaction').commit();
但是当我尝试重新加载事务时,我得到一个错误,如下
Uncaught Error: Attempted to handle event `loadedData` on <App.Answer> while in state rootState.loaded.updated.uncommitted. Called with undefined
现在,当我删除重新加载时,我不再收到错误,当我在网络选项卡下检查 Chrome 的控制台时,我可以看到我想要的结果正在被发回,但它们没有在我的DS 模型。答案未定义。
有人知道为什么会这样吗?我是否使用了错误的交易?
编辑
Application.SearchController = Ember.ObjectController.extend({
isComplete: function () {
return this.get('content.answers.length') !== 0;
},
search: function () {
this.get('content.transaction').commit();
var record = this.get('content');
var interval = setInterval(function (controller) {
if (controller.get('isComplete')) {
controller.transitionToRoute("search.view");
clearInterval(interval);
} else {
record.reload();
}
}, 5000, this);
}
});
所以基本上在我的路线中完成了一些工作来设置我的模型并将它们设置为内容,模型有一个 id 将在服务器端使用并与搜索结果一起发回然后添加到“答案”。
这个工作正常,直到找到多个结果。然后创建一个新模型,并在具有不同内容的不同控制器上再次调用搜索函数。这次轮到上线 记录.reload();
我得到了错误
未捕获的错误:尝试在 rootState.loaded.updated.uncommitted 状态下处理事件 loadedData。未定义调用
所以服务器仍然以正确的结果响应,但“答案”没有在客户端更新。
【问题讨论】:
标签: ember.js ember-data