【问题标题】:EXTjs access data set in one controller from anotherEXTjs 从另一个控制器访问一个控制器中的数据集
【发布时间】: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


【解决方案1】:

经过反复试验,这就是最终奏效的方法

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

    }
});

【讨论】:

    【解决方案2】:

    这样做 - 制作你的控制器

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

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

    MyAppName.app.getController('Training.myController').testVar

    【讨论】:

      猜你喜欢
      • 2018-06-15
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多