Roh*_*ore 0 sencha-touch sencha-touch-2
我在初始化视图的一部分中声明了一个变量.当我尝试在面板中声明的函数中访问此变量时,我收到一个错误,即变量未定义.我尝试通过1> this.variablename访问它2> viewid.variablename ....我做错了什么?
Ext.define('app.view.location', {
extend : 'Ext.Panel',
xtype : 'location_d',
id : 'locdetail',
initialize : function() {
loc = 'abc';
},
config : {
layout : {
type : 'card'
},
scrollable : true,
fullscreen : true,
items : [{
xtype : 'panel',
html : 'Get Dir',
id : 'Dir',
cls : 'loc',
listeners : {
tap : {
element : 'element',
fn : function(m) {
alert(this.loc); //gives me undefined variable error
}
}
}
}]
}
});
Run Code Online (Sandbox Code Playgroud)
小智 6
您的代码中存在一些错误.
首先,您需要在配置中添加loc als变量.
config : {
layout : {
type : 'card'
},
loc: null,
scrollable : true,
...}
Run Code Online (Sandbox Code Playgroud)
然后你应该用他的二传手设置loc.
initialize : function() {
this.setLoc("abc");
},
Run Code Online (Sandbox Code Playgroud)
在您的tap-listener中,您无法使用它,因为它引用内部面板而不是外部,使用ComponenetManager来获取外部面板.
listeners : {
tap : {
element : 'element',
fn : function(m) {
alert(Ext.ComponentManager.get("locdetail").getLoc());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试了一下,它没有问题!
完整代码:
Ext.define('app.view.location', {
extend : 'Ext.Panel',
xtype : 'location_d',
id : 'locdetail',
initialize : function() {
this.setLoc("abc");
},
config : {
layout : {
type : 'card'
},
loc: null,
scrollable : true,
fullscreen : true,
items : [{
xtype : 'panel',
html : 'Get Dir',
id : 'Dir',
cls : 'loc',
listeners : {
tap : {
element : 'element',
fn : function(m) {
alert(Ext.ComponentManager.get("locdetail").getLoc());
}
}
}
}]
}
});
Run Code Online (Sandbox Code Playgroud)
Sencha Touch Class系统指南:http://docs.sencha.com/touch/2-1/#!/guide/ class_system
| 归档时间: |
|
| 查看次数: |
4869 次 |
| 最近记录: |