Javascript EXTjs从一个控制器访问另一个控制器中的数据集
我有一个视图,它有一些子视图(它们都扩展了Ext.grid.Panel)。父视图的控制器在子视图的控制器中设置要访问的变量。我尝试在父控制器的init函数中设置一个变量。然后我尝试在子页面的控制器的单击函数中读取它的值。但这又抛出了一个未捕获的引用错误 我该怎么做 我试过这样的衣服。我正在尝试获取Javascript EXTjs从一个控制器访问另一个控制器中的数据集,javascript,extjs,extjs5,Javascript,Extjs,Extjs5,我有一个视图,它有一些子视图(它们都扩展了Ext.grid.Panel)。父视图的控制器在子视图的控制器中设置要访问的变量。我尝试在父控制器的init函数中设置一个变量。然后我尝试在子页面的控制器的单击函数中读取它的值。但这又抛出了一个未捕获的引用错误 我该怎么做 我试过这样的衣服。我正在尝试获取警报(MyApp.app.getController('MyApp.controller.Whatever').myVar)开始工作 //CHILD //Controller Ext.define('
警报(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.app.Controller”而不是“Ext.app.ViewController”
然后尝试使用相同的代码访问
MyAppName.app.getController('Training.myController').testVar经过反复试验,这就是最终成功的方法
//CHILD
//Controller
Ext.define('MyApp.controller.child', {
extend: 'Ext.app.ViewController',
alias: 'controller.child',
});
//View
Ext.define('MyApp.view.child', {
extend: 'Ext.form.field.Text',
alias: 'widget.child',
controller: 'child',
title: 'Alien',
listeners: {
focus: function(comp) {
alert(MyApp.app.getController('MyApp.controller.Whatever').getMyVar());
}
},
renderTo: Ext.getBody()
});
//----------
//PARENT
//Controller
Ext.define('MyApp.controller.Whatever', {
extend: 'Ext.app.ViewController',
alias: 'controller.Whatever',
myVar: "oldValue",
init: function() {
myVar = "newValue";
},
doInit: function() {
//THIS METHOD IS NECESSARY!!!
},
getMyVar: function() {
return myVar;
},
});
//View
Ext.define('MyApp.view.Whatever', {
extend: 'Ext.form.Panel',
alias: 'widget.Whatever',
controller: 'Whatever',
title: 'Hello',
items: [{
xtype: 'child'
}],
renderTo: Ext.getBody()
});
//------------------------
Ext.application({
name: 'MyApp',
launch: function() {
Ext.create('MyApp.view.Whatever');
}
});
我们需要更多的信息。您想在哪个版本的ExtJS中实现这一点?您有任何示例代码吗?你还做了什么其他的研究。。。是否或可能帮助您?也许您可以使用<代码>Ext.ComponentQuery.query('parentController')[0].fireEvent('yourEventName')在子控制器中,然后在父控制器中注册该事件(yourEventName),并在函数中返回变量。