javascript - Backbone get not working with "this"? -
i struggled use underscore backbone collection.
var collection=backbone.collection.extend({ model:somemodel, getmodelentry : function(id){ return this.get(id); //returns undefined } })
attempt 2:
var collection=backbone.collection.extend({ model:somemodel, getmodelentry : function(id){ var model = this.where({id:id})[0]; //here got model return model.get("attr"); //returns undefined } });
whats wrong using in collection?
get working perfect on instances!
var coll=new collection; coll.get(id); //working fine
works fine far see. check id of model you're looking exists in collection. add below , see happens
getmodelentry : function(id){ var model = this.get(id); if(model == undefined) { console.log("id: ",id); console.log("collection: ",json.stringify(this.models)); } else { console.log(model.get('name')); } }
Comments
Post a Comment