Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 余烬数据事务错误_Ember.js_Ember Data - Fatal编程技术网

Ember.js 余烬数据事务错误

Ember.js 余烬数据事务错误,ember.js,ember-data,Ember.js,Ember Data,我对余烬数据事务有问题 我有一个像这样的DS模型 App.MyModel = DS.Model.extend({ id: DS.attr(), answers: DS.hasMany('App.Answer') // another model }); 然后像这样在a路径中稍后启动 model: function(){ var transaction = this.get('store').transaction(); return transactio

我对余烬数据事务有问题

我有一个像这样的DS模型

App.MyModel = DS.Model.extend({
    id: DS.attr(),
    answers: DS.hasMany('App.Answer') // another model 
});
然后像这样在a路径中稍后启动

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
因此,基本上我的路线中做了一些工作来设置我的模型并将它们设置为内容,模型有一个id,将在服务器端使用,并将搜索结果发送回,然后添加到“答案”中

在找到多个结果之前,这项工作不会有问题。然后创建一个新模型,并在不同的控制器上使用不同的内容再次调用搜索函数。这次轮到我了 record.reload()

我得到了错误 未捕获错误:试图在状态为rootState.loaded.Update.uncommitted时处理事件
loadedData
。用未定义的


因此,服务器仍然会以正确的结果进行响应,但“答案”不会在客户端更新。

您的
MyModel
记录会在本地修改(客户端)。调用
reload
将尝试更新它,这在记录的当前状态下是禁止的

您可以使用以下命令进行检查:

console.log( this.get('content.stateManager.currentState.path') );
this.get('content').reload();
这将在控制台中显示记录处于未提交状态

更新:

你不能用定时器。一切都是异步的,您不能保证您的模型会在该时间间隔内更新。这意味着,在提交记录时,您可以同时重新加载它(这将生成您看到的错误)

你想要的是这样的:

Application.SearchController = Ember.ObjectController.extend({
    search: function () {
        var record = this.get('content'),
            controller = this;

        record.one('didCommit', function() {
            controller.transitionToRoute("search.view"); 
        });

        record.transaction.commit();
    }
});

在第一次提交后,事务被放置在默认事务上


记住一定要先安装路由器。

我自己也得出了这个结论。错误消息基本上告诉我。有没有解决这个问题的方法,我使用的是事务吗?这里没有足够的代码来指出真正的问题。我不知道您在应用程序中如何修改
app.MyModel
记录。在
commit
之后,您用来调用
reload
的逻辑似乎很危险。这样做时,我必须使用一个时间间隔,因为服务器刚刚被请求撞击。搜索总是有答案吗?后端的搜索是异步的吗?
Application.SearchController = Ember.ObjectController.extend({
    search: function () {
        var record = this.get('content'),
            controller = this;

        record.one('didCommit', function() {
            controller.transitionToRoute("search.view"); 
        });

        record.transaction.commit();
    }
});