首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TodoMVC - Ember.run.once

TodoMVC - Ember.run.once
EN

Stack Overflow用户
提问于 2013-03-28 15:57:08
回答 1查看 3.5K关注 0票数 6

我一直在研究Todo MVC App for Ember。在模型中,我注意到对包装在Ember.run.once中的commit()方法的调用请参阅:https://github.com/addyosmani/todomvc/blob/gh-pages/architecture-examples/emberjs/js/models/todo.js#L9

代码语言:javascript
复制
todoDidChange: function () {
    Ember.run.once(this, function () {
        this.get('store').commit();
    });
}.observes('isCompleted', 'title');

Ember.run.once中包装this.get('store').commit()有什么帮助呢?我将方法改为直接执行以下操作:

代码语言:javascript
复制
todoDidChange: function () {
    this.get('store').commit();
}.observes('isCompleted', 'title');

但我看不出有什么明显的区别。我读过the documentation,但一个previos SO discussion还没能弄明白。

这是不是因为它只是一个小应用程序而没有显示出区别?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-03 15:27:40

我在response to another question中找到了答案。

如果在数组的每一项上都有一个侦听器,如下所示:

代码语言:javascript
复制
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');
    }
});

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15676726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档