我有一个视图,其中包含一些子视图(它们都扩展了Ext.grid.Panel)。父视图的控制器设置了一个我想在子视图的控制器中访问的变量。我尝试在父控制器的init函数中设置变量。然后,我尝试在子页面的控制器的click函数中读取其值。但这随后引发了一个未捕获的ReferenceError。
我该怎么做呢?
我曾在Sencha小提琴上尝试过类似的方法。我正在努力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');
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句