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

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -