deleteRecord后列表未更新

Fré*_*aro 13 ember.js ember-data

我有一个ArrayController,其内容在这样的路由中定义:

App.UsersRoute = Ember.Route.extend({
  model: function() {
    return App.User.find();
  },

  setupController: function(controller, model) {
    this._super(controller, model);
    this.controllerFor('application').set('currentRoute', 'users');
  }
});
Run Code Online (Sandbox Code Playgroud)

我在模板中列出数据:

<ul>
  {{#each user in arrangedContent}}
  <li>
    {{user.lastName}} {{user.firstName}}
    {{#linkTo "users.edit" user class="btn btn-primary btn-small"}}EDIT{{/linkTo}}
  </li>
  {{/each}}
</ul>
Run Code Online (Sandbox Code Playgroud)

它工作正常.

如果我创建一个新项目,它会自动添加到模板中的列表中:

App.UsersNewRoute = Ember.Route.extend({
  model: function() {
    return App.User.createRecord({firstName: '', lastName: ''});
  }
});
Run Code Online (Sandbox Code Playgroud)

但是当我在"编辑"视图中删除某个项目时,它不起作用:

App.UsersEditController = Ember.ObjectController.extend({
  ...

  destroy: function() {
    this.get('content').deleteRecord();
    this.get('store').commit();
    this.transitionToRoute("users.index");
  }
});
Run Code Online (Sandbox Code Playgroud)

但是在"新"视图中,如果我删除了新创建的项目,它就可以工作(没有提交).

在编辑控制器中,如果我删除"提交",则列表会更新,但当我执行其他操作时,将重新加载列表,并重新显示已删除的项目(正常).

那么,如何删除项目?

注意:我使用ember和ember-data的"master"代码,刚刚刷新.

Jak*_*old 1

您还应该仅在删除记录后进行转换,因为提交是异步发生的。

var user = this.get("content");

user.one("didDelete", this, function() {
  this.transitionTo("users.index");
});

user.deleteRecord();
user.get("transaction").commit();
Run Code Online (Sandbox Code Playgroud)

另请注意,提交事务优于提交存储。如果您稍后决定将记录添加到它自己的事务中,您将需要做更少的工作,如果您不这样做,它仍然会使用与defaultTransaction提交存储相同的方式。