我目前正在学习骨干,并试图包裹我的头脑为什么需要初始化函数视图和集合?
这是我的一些代码:
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", },本教程的创建者决定使用此“初始化”属性,但初始化从未用作任务,如Tasks.initialize或AppView.initialize。 我尝试更改属性的名称,但它不起作用。 这是骨干中的保留字还是关键字? 为什么需要? 谢谢!
I am currently learning backbone and am trying to wrap my head why an initialize function is needed Views and collections?
Here is some of my code:
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", },The creator of the tutorial decided to use this 'initialize' property, but initialize is never used as a property such as Tasks.initialize or AppView.initialize. I tried changing the name of the property but it does not work. Is this a reserved word or key word in backbone? Why is it needed? Thanks!
最满意答案
Initialize是一个特殊功能(即“保留”)。 如果它是在视图/模型/集合上定义的,它将在视图/模型/集合实例化时执行(即,当您调用new )。 因此,如果您更改其名称,Backbone将不再自动调用它...
Initialize is a special function (i.e. "reserved"). If it is defined on a view/model/collection, it will be executed when the view/model/collection is instanciated (i.e. when you call new). So if you change its name, it won't be called automatically by Backbone anymore...
更多推荐
发布评论