简体   繁体   中英

Anybody can help me analyze these code the backbone each function meanings?

Now , we can assume the app.Todos is a Collection. Then assume we had trigger the filterAll function .

 filterOne : function (todo) { console.log(todo); todo.trigger('visible'); }, filterAll : function () { console.log(app.Todos); app.Todos.each(this.filterOne, this); }, 

After i had read the underscore doc about each , they say each_.each(list, iterator, [context]) , Iterates over a list of elements, yielding each in turn to an iterator function.

But the filterAll function use each to iterator a function this.filterOne ? So what's meaning ? This filterOne is not a list a elements , please help me .

Thanks

From Underscore Documentation you see _.each as follows

_.each(list, iterator, [context]) 

Here list can also correspond to models.

So this can be written as

                       `app.Todos.each(function() { } , this);`

                                      **OR**

                       _.each(app.Todos.models, function() { } , this);

So this is equivalent to

app.Todos.each(function(todo) {
     console.log(todo);
     todo.trigger('visible');
}, this);

OR

_.each(app.Todos.models, function(todo) {
         console.log(todo);
         todo.trigger('visible');
 }, this);

This filterOne is not a list a elements , please help me .

The underscore functions are implemented as methods on the backbone instances. So your

app.Todos.each(this.filterOne, this);

is equivalent to

_.each(app.Todo.models, this.filterOne, this);

or underscore's object wrapper :

_(app.Todo.models).each(this.filterOne, this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM