javascript - 在Extjs中,如何从另一个 Controller 调用 View 中的函数?

标签 javascript extjs extjs5

我正在尝试让它在 Sencha Fiddle 中工作。我面临的问题是我在这一行遇到错误

MyApp.app.getView('MyApp.view.test2').test();

当您在文本框内单击时,它会失败并显示错误(在控制台日志中) Uncaught TypeError: MyApp.app.getView(...).test is not a function

//Controller
Ext.define('MyApp.controller.test', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.test',
    myVar:0,
    init: function() {
    }
});

//View
Ext.define('MyApp.view.test', {
    extend: 'Ext.form.field.Text',
    alias:'widget.test',
    controller: 'test',
    title: 'Hello',   
    listeners: {
        focus: function(comp){
            MyApp.app.getView('MyApp.view.test2').test(); //Fails with an error that test is not a function                        }
    },
    renderTo: Ext.getBody()
});

//View
Ext.define('MyApp.view.test2', {
    extend: 'Ext.form.Panel',
    alias:'widget.test2', 
    title: 'Hello2',     
    renderTo: Ext.getBody(),
    test:function()
    {
        alert('in MyApp.view.test2');
    }
});

Ext.application({
    name: 'MyApp',
    launch: function() {
        Ext.create('MyApp.view.test');
        Ext.create('MyApp.view.test2');
    }
});

最佳答案

getView() 函数仅返回,而不是 View 的实例。请参阅ExtJs 5.1.1 Controller.getView() :

Returns a View class with the given name. To create an instance of the view, you can use it like it's used by Application to create the Viewport:

this.getView('Viewport').create();

要获取创建的 View 实例,您可以使用 Ext.ComponentQuery 将其存档.

//query returns an array of matching components, we choose the one and only
var test2View = Ext.ComponentQuery.query('test2')[0];
// and now we can execute the function
test2View.test();

查看运行简约 fiddle .

//View
Ext.define('MyApp.view.test', {
    extend: 'Ext.form.field.Text',
    alias: 'widget.test',
    title: 'Hello',
    listeners: {
        focus: function (comp) {
            //query returns an array of matching components, we choose the one and only
            var test2View = Ext.ComponentQuery.query('test2')[0]; 
            test2View.test();
            //MyApp.app.getView('MyApp.view.test2').test();//Fails with an error that test is not a function
        }
    },
    renderTo: Ext.getBody()
});


//View
Ext.define('MyApp.view.test2', {
    extend: 'Ext.form.Panel',
    alias: 'widget.test2',
    title: 'Hello2',
    renderTo: Ext.getBody(),
    test: function ()
    {
        alert('in MyApp.view.test2');
    }
});


Ext.application({
    name: 'MyApp',
    launch: function () {
        Ext.create('MyApp.view.test');
        Ext.create('MyApp.view.test2');
    }
});

关于javascript - 在Extjs中,如何从另一个 Controller 调用 View 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32151733/

相关文章:

javascript - 如何扩展配置值

javascript - 您可以更新 webkitNotifications 吗?

javascript - 在 JavaScript 中将变量放在 for 循环内部而不是外部有什么意义?

javascript - 如何在 window.resize 上动态调整 3 个 div 的大小?

javascript - Extjs 5将 Accordion 按钮移动到标题左侧

javascript - Extjs如何解析jsonP响应

javascript - 为组成父组件的每个项目创建一个单独的组件?

javascript - 在 Firefox 中动态加载 JQuery 时不稳定

javascript - 如何更改 extjs 中特定列中的所有值?

html - 取消设置 td 填充