I have a problem with collections in backbone.js, I'm doing something wrong. This is my Model
var Point = Backbone.Model.extend({
defaults: {
x: 0.0,
y: 0.0,
z: 0.0,
},
validate: function(attrs) {
if(!isNumber(attrs.x) || !isNumber(attrs.y) || !isNumber(attrs.z))
return "Discovered NaN point";
else return null;
},
});
my collection:
var TrackCollection = Backbone.Collection.extend({
model: Point,
});
And the view:
var TrackView = Backbone.View.extend({
initialize: function() {
_bind('all', this.render, this);
},
render: function() {
console.log(this.collection.models);
return this
},
});
In my home view I do:
var c = new TrackCollection();
c.add({ x: 1, y: 1, z: 1});
c.add({ x: 1, y: 1, z: 1});
c.add({ x: 1, y: 1, z: 1});
var v = new TrackView({
collection: c,
});
v.render();
Why the console.log(this.collection.models); in the render method returns an empty array ? Instead if I change it into console.log(this.collection); it returns my collection object..
EDIT Here the home view
window.HomeView = Backbone.View.extend({
initialize: function() {
},
events: {
"click #makeIT": "makeIT",
},
render: function() {
$(this.el).html(this.template());
return this;
},
makeIT: function(e) {
var c = new TrackCollection();
c.add({ x: 1, y: 1, z: 1});
c.add({ x: 1, y: 1, z: 1});
c.add({ x: 1, y: 1, z: 1});
var v = new TrackView({
collection: c
});
v.render();
}
});
If I copy the makeIT content and put it in the same file where are the model and collection it works else no