TodoMVC - Ember.run.once

I worked on the Todo MVC app for Ember . In the model, I noticed a call to the commit () method enclosed in Ember.run.once See: 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'); 

How does wrapping this.get('store').commit() in Ember.run.once help? I changed the method to just do:

 todoDidChange: function () { this.get('store').commit(); }.observes('isCompleted', 'title'); 

But I do not see any apparent difference. I read the documentation and prev SO discussion could not do this.

Is this a case where the difference is not displayed because it is just a small application?

+6
source share
1 answer

I found the answer as an answer to another question .

If you have a listener for each element of the array, for example:

 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'); } }); 

Without Ember.run.once , this.categorize() will be called for each item managed in the list. If the three elements are changed, then there will be three calls. With the category wrapped in Ember.run.once , it will only be called once at the end of the chain.

+5
source

All Articles