Backbone.js的 - 传递参数的构造函数,通过 [英] Backbone.js - passing arguments through constructors
问题描述
情景:结果
我得到了一个警报()
说未定义
当我(尝试)设置 myVar的
通过构造函数的变量。但是如果我取消注释 myVar的
是这样的MyView的生活里面,该警报则不用说你好从内,正如人们所期望的。
问:结果
这是否意味着我不能设置任何PARAMS在视图中,除了骨干网构造自己的参数,可以如模式
,集
,报
, ID
,的className
&安培; 标签名
?结果
的手动:<一href=\"http://documentcloud.github.com/backbone/#View-constructor\">http://documentcloud.github.com/backbone/#View-constructor
的code:
VAR MyView的= Backbone.View.extend({ // myVar的:你好从内', 初始化:功能(){
警报(this.myVar);
}
)};新的MyView的({myVar的:你好从外面'});
选项传递到构造函数会被自动存储为 this.options
VAR MyView的= Backbone.View.extend({ myVar的:你好从内', 初始化:功能(){
警报(this.options.myVar);
}
)};新的MyView的({myVar的:你好从外面'});
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'});
Options passed into the constructor are automatically stored as this.options
var myView = Backbone.View.extend({
myVar : 'Hello from inside',
initialize: function() {
alert(this.options.myVar);
}
)};
new myView({myVar: 'Hello from outside'});
这篇关于Backbone.js的 - 传递参数的构造函数,通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!