在集合中初始化属性(Initialize property in Collections)

编程入门 行业动态 更新时间:2024-10-22 21:40:43
在集合中初始化属性(Initialize property in 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", },

本教程的创建者决定使用此“初始化”属性,但初始化从未用作任务,如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...

更多推荐

本文发布于:2023-08-07 19:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   属性   Initialize   property   Collections

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!