【发布时间】:2015-08-22 08:35:04
【问题描述】:
我有一个包含一些子视图的视图(它们都扩展了 Ext.grid.Panel)。父视图的控制器设置了一个我想在子视图的控制器中访问的变量。我尝试在父控制器的 init 函数中设置一个变量。然后我尝试在子页面控制器的点击功能中读取它的值。但这引发了一个未捕获的 ReferenceError。
我该怎么做?
我在Sencha fiddle 上尝试过类似的操作。我正在尝试让alert(MyApp.app.getController('MyApp.controller.Whatever').myVar); 工作
//CHILD
//Controller
Ext.define('MyApp.controller.child', {
extend: 'Ext.app.ViewController',
alias: 'controller.child',
init: function() {
alert("Initializing Child");
}
});
//View
Ext.define('MyApp.view.child', {
extend: 'Ext.form.field.Text',
alias:'widget.child',
controller: 'child',
title: 'Alien',
width: 200,
listeners: {
focus: function(comp){
alert(MyApp.app.getController('MyApp.controller.Whatever').myVar);
}
},
renderTo: Ext.getBody()
});
//----------
//PARENT
//Controller
Ext.define('MyApp.controller.Whatever', {
extend: 'Ext.app.ViewController',
alias: 'controller.Whatever',
myVar:0,
init: function() {
alert("initializing parent");
myVar=20;
}
});
//View
Ext.define('MyApp.view.Whatever', {
extend: 'Ext.form.Panel',
alias:'widget.Whatever',
controller: 'Whatever',
title: 'Hello',
items:[{
xtype: 'child'
}],
width: 200,
renderTo: Ext.getBody()
});
//------------------------
Ext.application({
name: 'MyApp',
launch: function() {
Ext.create('MyApp.view.Whatever');
}
});
【问题讨论】:
-
我们需要更多信息。您尝试在哪个版本的 Ext JS 中完成此操作,您有任何示例代码吗?您还做了哪些其他研究...this thread 或可能this thread 对您有帮助吗?
-
也许你可以使用。
Ext.ComponentQuery.query('parentController')[0].fireEvent('yourEventName');在您的子控制器中,然后在父控制器中注册该事件(yourEventName)并在函数中返回您的变量。
标签: javascript extjs extjs5