EXTjs从一个控制器访问另一个控制器中的数据集

时间:2015-08-21 15:34:16

标签: javascript extjs extjs5

我有一个包含一些子视图的视图(所有视图都扩展为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');

    }
});

2 个答案:

答案 0 :(得分:0)

通过反复试验,这就是最终的工作

//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');

    }
});

答案 1 :(得分:-1)

执行此操作 - 制作控制器

'Ext.app.Controller'而不是'Ext.app.ViewController'

然后尝试使用相同的代码访问

MyAppName.app.getController( 'Training.myController')。的testvar

相关问题