Reputation: 253
I am writing a new backbone application, and I am getting some errors on code that I have working in other applications. I am using the newest version of backbone for this application, so I do not know if there have been changes that make what I am doing here break, or what. Google has not been any help to me all day.
I have a collection that I am breaking apart, and sending the individual models to views. The views report that this.model is undefined.
Code:
The view:
DigitalAnalytics.View.TopPage = Backbone.Model.extend({
tagName: 'tr',
className: 'db-toppage-tr',
template: DigitalAnalytics.Helper.template('db-analytics-toppages-template'),
initialize: function() {
console.log(this.model); // prints 'undefined'
},
render: function() {
console.log(this.model); // prints 'undefined'
// This throws an error saying that it can not call html() on undefined
this.$el.html(this.template( this.model.toJSON()) );
return this;
},
events: {}
});
The collection view:
DigitalAnalytics.View.Collection.TopPages = Backbone.View.extend({
el: $('#db-analytics-toppages'),
template: DigitalAnalytics.Helper.template(''),
initialize: function() {
this.listenTo(this.collection, 'reset', this.render); // Never fires even though the collection is returned
this.collection.fetch(); // Works just fine
},
render: function() {
this.collection.each( function(page, index) {
console.log(page.toJSON()); // writes the object to console just fine
var topPage = new DigitalAnalytics.View.TopPage({model: page});
this.$el.append( topPage.render().el );
}, this);
}
});
Upvotes: 0
Views: 55
Reputation: 845
You probably have extended Backbone.Model
where you should have extended Backbone.View
Change Backbone.Model.extend
in your code to Backbone.View.extend
, create a Backbone model using Backbone.Model
then pass this model to view when you initiate it.
Upvotes: 2