6

I've been working on the Todo MVC App for Ember. In the model, I noticed a call to a commit() method wrapped 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 dont see any visible difference. I read the documentation and a previos SO discussion haven't been able to figure it out.

Is this a case where the difference doesn't show because it's just a small app?

Community
  • 1
  • 1
Rushi
  • 1,652
  • 3
  • 17
  • 24

1 Answers1

5

I found the answer as a response to another question.

If you have a listener on each item of an array like this:

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 manipulated in the list. If three items are modified, then there will be three calls. With categorize wrapped in Ember.run.once , it will be only called once at the end of the chain.

Community
  • 1
  • 1
Rushi
  • 1,652
  • 3
  • 17
  • 24