Reputation: 23
I am on the learning curve here so pardon me if my question seems trivial. I am trying to create a collection in backbone. The problem is when I try to console.log the teamGroup collection, I get an undefined error. Any reason why this is happening?
var team = Backbone.Model.extend({
defaults : {
name: "",
role: "",
bio: "",
link: "",
media: ""
}
});
//Create a collection template that references which model to base it on
var teamCollection = Backbone.Collection.extend({
model: team
});
//Create model instances for each team member
var teamPayal = new team({
name: "P Seth",
role: "Director",
bio: "Director PS is from Hyberadad.",
link: "www.filmschool.com",
media: "www.facebook.com"
});
var teamUnio = new team({
name: "Unio Guiterrez",
role: "Producer",
bio: "Unio is a producer from SF",
link: "www.greengeender.com",
media: "www.twitter.com/unio"
});
var teamScott = new team({
name: "Scott Baskovich",
role: "Musician",
bio: "Scott is a musician from SF",
link: "www.music.com",
media: "www.linkedin.com"
});
//Create new collection for the team
var teamGroup = teamCollection([
teamPayal, teamUnio, teamScott
]);
console.log(teamGroup.toJSON());
</script>
Upvotes: 0
Views: 39
Reputation: 1553
You forgot new
before teamCollection
to create new instance of backbone collection
var teamGroup = new teamCollection([
teamPayal, teamUnio, teamScott
]);
There is a convention used by some JavaScript programmers to always start the names of functions constructors with a capital letter (TeamCollection
) and to always start all instances names with a lowercase letter (var team = new Team({...})
).
Upvotes: 4