我研究 Backbone n underscore。
有人帮帮我。
我不知道 _.each() 的模型未定义。
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.
为什么模型未定义???
/*underscoreJS model is undefined...*/
_.each(todos, function (model) {
console.log(model.get('title'));
});
最佳答案
对于下划线,你需要这样做:
_.each(todos.models, function(model) {
console.log(model.get('title'));
});
你这样做的原因
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 返回一个代理对象,它在 todos.models
工作代码here
关于javascript - underscore _.each 和 forEach 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36351032/