javascript - Backbone collection.each() is not working -


i receiving following message when try use backbone's collection.each() method:

typeerror: object function (){ return parent.apply(this, arguments); } has no method 'each'.

i'm learning backbone jeffrey way tutorials; entered identical code his, reason doesn't work.

var person = backbone.model.extend({     defaults: {         name: 'besim dauti',         age: 15,         occupation: 'web developer'     } });  var peopleview = backbone.view.extend({     tagname: 'ul',      render: function(){         this.collection.each(function(person) {             var personview = new personview({ model: person });             this.$el.append(personview.render().el)         }, this)         return this;     } });  var personview = backbone.view.extend({     tagname: 'li',      template: _.template( $('#persontemplate').html() ),      render: function(){         this.$el.html(this.template(this.model.tojson()) );         return this;     } });  var peoplecollection = backbone.collection.extend({     model: person });  var peoplecollection = new peoplecollection([     {         name: 'besim',         age: 25,         occupation: 'web developer'     },     {         name: 'gaurav',         age: 25,         occupation: 'web designer'     },     {         name: 'jeffry',         age: 27     } ])  var peopleview = new peopleview ({collection: peoplecollection}); $(document.body).append(peopleview.render().el); 

why there problem .each() function? typed code identically and, jeffrey, worked expected, me displayed error. appreciate help.

you need initialise peoplecollection.

see: var peopleview = new peopleview ({collection: peoplecollection});

you passing in raw constructor , not instance of it.

you should this:

var peoplecollection = new peoplecollection(); // create instance of peoplecollection var peopleview = new peopleview ({collection: peoplecollection}); // pass peopleview 

if familiar oop (object orientated programming) think of peoplecollection class , var peoplecollection = new peoplecollection() instance of class. must work instances.

you encounter error inside .each because capitalised person. change to:

this.collection.each(function(person) {    var personview = new personview({ model: person });    this.$el.append(personview.render().el) }, this) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -