javascript - Backbone each undefined -
why item variable undefined in backbone example?
var action = backbone.model.extend({ defaults: { "selected": false, "name": "first action", "targetdate": "10-04-2014" } }); var actions = backbone.collection.extend({ model: action }); var actioncollection = new actions( [new action(), new action(), new action(), new action()]); _.each(actioncollection, function(item) { alert(item); });
jsfiddle here: http://jsfiddle.net/netroworx/klyl9/
change to:
actioncollection.each(function(item) { alert(item); });
and works fine.
this because actioncollection not array, _.each(collection) not work collection.each because function build backbone collection.
that being said, works:
_.each(actioncollection.tojson(), function(item) { alert(item); });
because collection actual array.
Comments
Post a Comment