我一直在研究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
todoDidChange: function () {
Ember.run.once(this, function () {
this.get('store').commit();
});
}.observes('isCompleted', 'title');在Ember.run.once中包装this.get('store').commit()有什么帮助呢?我将方法改为直接执行以下操作:
todoDidChange: function () {
this.get('store').commit();
}.observes('isCompleted', 'title');但我看不出有什么明显的区别。我读过the documentation,但一个previos SO discussion还没能弄明白。
这是不是因为它只是一个小应用程序而没有显示出区别?
发布于 2013-04-03 15:27:40
我在response to another question中找到了答案。
如果在数组的每一项上都有一个侦听器,如下所示:
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中的分类,它将只在链的末尾被调用一次。
https://stackoverflow.com/questions/15676726
复制相似问题