我正在尝试从组件初始化访问函数。 Sencha Touch / Ext.js的新手,所以不确定范围是如何工作的。我在'控制'中'旋转'听众似乎运行良好,增量时。
简化代码:
Ext.define('App.view.CreateChallenge', {
extend: 'Ext.Container',
fullscreen: true,
xtype: 'CreateChallenge',
config:{
items[
xtype: 'spinnerfield',
itemId: 'mySpin',
initialize:function(){
App.view.Challenge.doThis();
}
],
control:{
'#mySpin' : {
spin: 'doThis'
}
}
},
doThis:function(){
console.log('dooing this');
}
});
谢谢, 史蒂夫
答案 0 :(得分:2)
首先,你不应该在配置块中覆盖initialize
方法,正确的位置在Ext.define
。
然后,在initialize
中,您可以拨打App.view.CreateChallenge.doThis()
。但它只是一个普通函数,其范围是全局window
对象,因此您无权访问App.view.CreateChallenge
的对象。
要从子项中查找容器引用,可以使用Ext.Component.up()
。在您的情况下,它看起来像:
initialize: function() {
…
this.up('CreateChallenge').doThis(); // find container by xtype then call its method
}
我们还没有!如果您尝试,您将从undefined
获得this.up(…)
。这是因为容器在其子项初始化时未完成构造。因此,如果您需要在initialize
方法中引用容器,则需要等到容器准备就绪,例如painted
事件。
然后,你会得到一些看起来像:
initialize: function() {
…
this.on('painted', function () {
this.up('CreateChallenge').doThis(); // find container by xtype then call its method
}, this);
}
答案 1 :(得分:0)
我只想使用
VIEW/This .getParent()