我现在处于学习曲线上,因此,如果我的问题似乎微不足道,请原谅我。我正在尝试在骨干网中创建一个集合。问题是,当我尝试console.log teamGroup集合时,出现未定义的错误。为什么会这样呢?
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>
最佳答案
您在new
之前忘记了teamCollection
创建主干集合的新实例
var teamGroup = new teamCollection([
teamPayal, teamUnio, teamScott
]);
一些JavaScript程序员使用一种约定,始终以大写字母(
TeamCollection
)开头函数构造函数的名称,并始终以小写字母(var team = new Team({...})
)开头的所有实例名称。