我有一个包含一些 subview 的 View (所有 subview 都扩展了 Ext.grid.Panel)。父 View 的 Controller 设置了一个我想要在 subview 的 Controller 中访问的变量。我尝试在父 Controller 的 init 函数中设置一个变量。然后我尝试在子页面 Controller 的单击函数中读取它的值。但随后抛出了一个 Uncaught 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');
}
});
最佳答案
经过反复试验,这最终奏效了
//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');
}
});
关于javascript - EXTjs 从一个 Controller 访问另一个 Controller 中的数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32144256/