Reputation: 15293
I am very basic level with backbone.js, i am trying to add couple of elements to body, for that is used this function, but i am getting the erros as "Uncaught ReferenceError: age is not defined" - what is wrong with my code?
error i am getting:
"Uncaught ReferenceError: age is not defined"
HTML(template):
<script id="person" type="text/template">
<strong><%= name %></strong><sup><%= age %></sup>
</script>
function:
(function($){
var Model = Backbone.Model.extend({
defaults:{
name:'default name',
age:'default age'
}
});
var Col = Backbone.Collection.extend({
model:Model
});
var PersonView = Backbone.View.extend({
tagName:'li',
template:_.template($('#person').html()),
initialize:function(){
this.render();
},
render:function(){
$('body').append(this.$el.html(this.template(this.model.toJSON())));
}
});
var ncol = new Col({model:[{name:'abc',age:1},{name:'cdf',age:2},{name:'ghi',age:1}]});
var persons = new PersonView({model:ncol});
})(jQuery)
Any one help me to sort my issue..?
Upvotes: 1
Views: 175
Reputation: 35890
You are incorrectly initializing your collection. The Collection
constructor expects the initial models as an array, not an object. So instead of:
new Col({model:[
{name:'abc',age:1},
{name:'cdf',age:2},
{name:'ghi',age:1}
]});
It should be:
new Col([
{name:'abc',age:1},
{name:'cdf',age:2},
{name:'ghi',age:1}
]);
This may or may not be the cause your particular error, but that least this will cause some serious havoc.
Upvotes: 3