我目前正在学习骨干,并且想尽一切办法为什么需要初始化函数Views和collections?
这是我的一些代码:
Tasks = Backbone.Collection.extend({
//This is our Task collection and holds our Task models
initialize: function (models, options) {
console.log(options);
this.bind("add", options.view.addTaskListeners);
console.log(this.bind("add", options.view.addTaskListeners));
//Listen for new additions to the collection.
}
});
//master view
AppView = Backbone.View.extend({
el: $("body"),
initialize: function () {
this.tasks = new Tasks( null, { view: this });
// Create a task collection when the view is initialized.
// Pass it a reference to this view to create a connection between the two
},
events: {
"click #add-task": "showPrompt",
},
本教程的创建者决定使用此'initialize'属性,但initialize从未用作Tasks.initialize或AppView.initialize之类的属性。我尝试更改属性的名称,但是它不起作用。这是骨干网中的保留字还是关键词?为什么需要它?谢谢!
初始化是一个特殊功能(即“保留”)。如果它是在视图/模型/集合上定义的,则在实例化视图/模型/集合时(即,当您调用时new
)将执行它。因此,如果您更改名称,Backbone将不再自动调用该名称...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句