Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TodoMVC - Ember.run.once

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?

like image 297
Rushi Avatar asked Mar 28 '13 07:03

Rushi


1 Answers

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.

like image 101
Rushi Avatar answered Oct 11 '22 17:10

Rushi