TodoMVC - Ember.run.once

Rus*_*shi 6 ember.js todomvc

我一直在研究EmberTodo MVC应用程序.在模型中,我注意到调用了一个commit()方法,Ember.run.once请参阅:https://github.com/addyosmani/todomvc/blob/gh-pages/architecture-examples/emberjs/js/models/todo.js# L9

todoDidChange: function () {
    Ember.run.once(this, function () {
        this.get('store').commit();
    });
}.observes('isCompleted', 'title');
Run Code Online (Sandbox Code Playgroud)

如何包装this.get('store').commit()Ember.run.once帮助吗?我把方法改为:

todoDidChange: function () {
    this.get('store').commit();
}.observes('isCompleted', 'title');
Run Code Online (Sandbox Code Playgroud)

但我没有看到任何明显的差异.我阅读了文档,并且一个普遍的SO讨论无法弄清楚.

这是一个差异没有显示的情况,因为它只是一个小应用程序?

Rus*_*shi 5

我发现答案是对另一个问题的回答.

如果你对数组的每个项都有一个监听器,如下所示:

App.IssuesController = Ember.ArrayController.extend({ 
    issue_list: ['a','b','c'],
    issueListObserver : function(){
        Ember.run.once(this, this.categorize);
    }.observes('issue_list.@each"),

    this.categorize: function () {
        console.log('foo');
    }
});
Run Code Online (Sandbox Code Playgroud)

如果没有Ember.run.once,this.categorize()将调用列表中操作的每个项目.如果修改了三个项目,则会有三个调用.在分类包装的情况下Ember.run.once,它只会在链的末尾调用一次.