【问题标题】:Ember-Data transaction errorEmber 数据交易错误
【发布时间】: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


    【解决方案1】:

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

    Error Attempted to handle event `loadedData` : Object not updated after deleteRecord

    记住总是先设置路由器。

    【讨论】:

      【解决方案2】:

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

      你可以用一个命令来检查:

      console.log( this.get('content.stateManager.currentState.path') );
      this.get('content').reload();
      

      这应该会在您的控制台中显示该记录处于uncommitted 状态。

      更新:

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

      你想要的是这样的:

      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 的逻辑似乎很危险。
      • 我不得不使用一个间隔来这样做,服务器只是被请求撞击
      • 搜索总是有答案?后端搜索是异步的吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 2016-02-12
      • 2019-09-06
      • 1970-01-01
      相关资源
      最近更新 更多