【问题标题】:getting error when trying to update a model inside a ObjectController in Ember尝试在 Ember 中更新对象控制器中的模型时出错
【发布时间】:2013-10-03 07:05:22
【问题描述】:

页面加载从用户查看用户个人资料时开始。然后他做了一些动作,我的代码会调用 ajax 来更新它的用户类型-

App.UserController = Ember.ObjectController.extend
  convert: ->
    $.get '/api/user/convert_to_client/' + id, (response) ->
      self.set('user_type', response.user.user_type)     

但是每当我进入用户列表时,Ember 会尝试从 UsersRoute 中获取所有用户:

module.exports = App.UsersRoute = Em.Route.extend
    model: ->
      App.User.find({}).then (response) ->     
        self.controllerFor('users').set('content', response)       

然后我最终得到类似于这些的所有错误:

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

Update ember-data model\

我认为这篇文章在这里解释了这个问题 -

http://www.thomasboyt.com/2013/05/01/why-ember-data-breaks.html

未捕获的错误:尝试处理事件 loadedData on while in 状态 rootState.loaded.updated.uncommitted。使用 {}

调用

这意味着您正在尝试对记录做一些事情 它在当前状态下无法做到。尝试时经常发生这种情况 更新当前正在保存或尝试呈现的记录 已删除对象的属性。

但是请注意,当它反过来时,我先去用户列表,然后去查看用户的个人资料,更新用户 - 这个错误永远不会出现。

编辑:

用户回复示例:

{
  users: [
    {
       _id: 521e1112e8c5e10fb40002a0
        ..
    }
   ]
}

对于单个用户:

{
  user: {
    _id: 521e1116e8c5e10fb40004ca
  }
}

【问题讨论】:

  • 你能发布你得到的确切错误吗?
  • 这是错误 - 未捕获的错误:尝试在状态为 rootState.loaded.updated.uncommitted 时处理 上的事件 loadedData。未定义调用
  • 能否也包括您的模型定义以及两个请求的 JSON 响应?
  • 我在几个地方编辑了我的帖子 - 首先我添加了示例 json 结果,它符合 ember 数据的格式。 2nd - 一直向上滚动,您将看到在 UserController 内部进行了 m ajax 调用。

标签: ember.js ember-data ember-model


【解决方案1】:

你应该像这样在 `setupController' 钩子中设置你的控制器:

model: function() {
   return App.User.find({});
},
setupController: function(controller, model) {
   controller.set('content', model);
}

不确定这是否是唯一的问题。您能否发布有关您遇到的错误的更多信息?

【讨论】:

  • 我想你的意思是把它放在 UserRoute 上吧?没有帮助 - 在 UsersRoute 上也有。
  • 这应该在您的 Users 路由中,因为您在模型挂钩中获取 所有 用户
  • 我在这两个地方也都有。总结我的问题。我想更新单个模型的数据,然后进入模型列表页面,没有任何错误。
【解决方案2】:

您应该使用store.update 来更新存储中的记录而不更改记录的状态(而不是在记录上使用set),例如

$.get '/api/user/convert_to_client/' + id, (response) =>
  @store.update 'user', id: id, user_type: response.user.user_type

或者,如果您的回复结构正确,只需:

@store.update 'user', response.user

注意:update 在旧版本的 EmberData 中可能不可用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2015-08-16
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多