2016-04-01 7 views
3

Ich studiere Backbone n Unterstreichung.
jemand mir helfen.
Ich weiß nicht, dass das Modell von _.each() undefiniert ist.Was ist der Unterschied zwischen Unterstrich _.each und forEach?

var todos = new Backbone.Collection(); 

    todos.add([ 
     { title: 'go to Belgium.', completed: false }, 
     { title: 'go to China.', completed: false }, 
     { title: 'go to Austria.', completed: true } 
    ]); 

    // iterate over models in the collection 
    todos.forEach(function(model){ 
     console.log(model.get('title')); 
    }); 
    // Above logs: 
    // go to Belgium. 
    // go to China. 
    // go to Austria. 

Warum Modell ist nicht definiert ???

/*underscoreJS model is undefined...*/ 
    _.each(todos, function (model) { 
     console.log(model.get('title')); 
    }); 
+1

Backbone hat einen Proxy der '_.each' Methode, den Sie in Ihrem Fall verwenden müssen:' todos.each (function (todo) {...}) ' – hindmost

Antwort

0

Für unterstreichen Sie so tun müssen:

_.each(todos.models, function(model) { 
    console.log(model.get('title')); 
}); 

Grund, wenn Sie tun

var todos = new Backbone.Collection(); 

todos.add([{ 
    title: 'go to Belgium.', 
    completed: false 
}, { 
    title: 'go to China.', 
    completed: false 
}, { 
    title: 'go to Austria.', 
    completed: true 
}]); 

Backbone liefert ein Proxy-Objekt, das in todos.models

das Modell Array hält

Arbeitscode here