简体   繁体   English

deleteRecord后列表未更新

[英]List not updated after deleteRecord

I have an ArrayController whose content is defined in a route like that: 我有一个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');
  }
});

And I list the data in a template: 我在模板中列出数据:

<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>

It works fine. 它工作正常。

If I create a new item, it is automatically added to the list in the template: 如果我创建一个新项目,它会自动添加到模板中的列表中:

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

But when I delete an item in a "edit" view, it doesn't work: 但是当我在“编辑”视图中删除某个项目时,它不起作用:

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

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

But in the "new" view, if I delete the new created item, it works (without the commit). 但是在“新”视图中,如果我删除了新创建的项目,它就可以工作(没有提交)。

In the edit controller, if I remove the "commit", the list is updated, but when I do another action, the list is reloaded, and the deleted item reappears (normal). 在编辑控制器中,如果我删除“提交”,则列表会更新,但当我执行其他操作时,将重新加载列表,并重新显示已删除的项目(正常)。

So, how to delete an item? 那么,如何删除项目?

NOTE: I use the "master" code of ember and ember-data, refreshed just now. 注意:我使用ember和ember-data的“master”代码,刚刚刷新。

I just experienced a similar issue under a very different set of circumstances. 我刚刚在一组非常不同的情况下遇到了类似的问题。

I'm using " grunt-ember-boilerplate " (highly recommended if you're into CoffeeScript and Ember). 我正在使用“ grunt-ember-boilerplate ”(强烈建议你使用CoffeeScript和Ember)。 It came with a version of Ember Data that had a weird bug where properly deleted records would persist in the cache and thus not be removed from lists. 它附带了一个版本的Ember Data,它有一个奇怪的错误,正确删除的记录将保留在缓存中,因此不会从列表中删除。

I did't have the time to figure out exactly what was going on; 我没有时间弄清楚到底发生了什么; so I just tried getting the latest build of Ember Data (2013-05-10 10:20:34 -0700) and that fixed the issue right away. 所以我只是尝试了最新版本的Ember Data(2013-05-10 10:20:34 -0700)并立即解决了这个问题。

Just posting here in case anyone else comes across a similar issue. 只是发布在这里,万一其他人遇到类似的问题。

Also, I agree with Jakub Arnold when he talks about not committing the "store" and using event listeners to ensure clean state. 此外,我同意Jakub Arnold谈到不提交“商店”并使用事件监听器来确保清洁状态。 Just today, I found a very useful blog entry on the subject, " Patterns and anti-patterns for Ember Data ". 就在今天,我找到了一个关于这个主题的非常有用的博客文章,“ Ember Data的模式和反模式 ”。

You should also transition only after the record was deleted, since commit happens asynchronously. 您还应该仅在删除记录后进行转换,因为提交是异步发生的。

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

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

user.deleteRecord();
user.get("transaction").commit();

Also note that comitting the transaction is preferred over comitting the store. 另请注意,与商店相比,优先考虑交易。 If you later decide to add the record to it's own transaction you will have less work to do, and if you don't, it will still use the same defaultTransaction as comitting the store would. 如果你以后决定将记录添加到它自己的事务中,那么你将没有多少工作要做,如果你不这样做,它仍然会使用与商店相同的defaultTransaction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM