Backbone.js - 通过构造函数传递参数
场景:
当我(尝试)通过构造函数设置 myVar
变量时,我收到一个 alert()
提示 undefined
。但是,如果我取消注释 myView 内部的 myVar
,警报就会显示“Hello from inside”,正如人们所期望的那样。
问题:
这是否意味着我无法在视图的构造函数中设置除骨干网自己的参数之外的任何参数,例如 model
、collection
、el
、<代码>id, className
& 标签名称
?
手册:http://documentcloud.github.com/backbone/#View-constructor
代码:
var myView = Backbone.View.extend({
//myVar : 'Hello from inside',
initialize: function() {
alert(this.myVar);
}
)};
new myView({myVar: 'Hello from outside'});
Scenario:
I got an alert()
saying undefined
when I (try to) set the myVar
variable through the Constructor. However if I uncomment the myVar
that's living inside the myView, the alert then instead says "Hello from inside", just as one would expect.
Question:
Does this mean that I cannot set any params in the constructor of the view except backbones own params, such as model
, collection
, el
, id
, className
& tagName
?
Manual: http://documentcloud.github.com/backbone/#View-constructor
The code:
var myView = Backbone.View.extend({
//myVar : 'Hello from inside',
initialize: function() {
alert(this.myVar);
}
)};
new myView({myVar: 'Hello from outside'});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
传递到构造函数的选项会自动存储为
this.options
Options passed into the constructor are automatically stored as
this.options
从主干 1.1.0 开始,
options
参数不再自动附加视图(请参阅问题 2458 进行讨论)。您现在需要手动附加每个视图的选项:或者您可以使用这个迷你插件来自动-附加白名单选项,如下所示:
As of backbone 1.1.0, the
options
argument is no longer attached automatically to the view (see issue 2458 for discussion). You now need to attach the options of each view manually:Alternatively you can use this mini plugin to auto-attach white-listed options, like so: