Accessing functions in ViewController from form created on-the-fly

不羁的心 提交于 2019-12-13 05:59:09

问题


The view controller will be active, but it will be controlling one sub-view of many other views, so I think there will be other active view controllers.

By on-the-fly basically the form is created by a completely different dispatcher, with components and handlers, etc. with no direct connection between the view controller and the form.

The view controller has several functions I need to get results from:

canContinue: function (arg1) {var result = true; /*...*/ return result;}
getStatus: function (arg1, arg2) {var result = 0; /* ... */ return result;}

/*...*/ means code omitted. So I want access to the view controller and these functions. Something along these lines:

var controller = /* how to get it? */;

if (!controller.canContinue(p))
    alert(controller.status(q, r));

I read it is not recommended to access functions directly in this manner, so tried firing events, but I can't seem to get results back, which I need.

I thought I could give the view controller an itemId, and do Ext.ComponentQuery.query('#itemId'), but it doesn't seem to work. I guess a view controller isn't a component, makes sense.

The view controller has a unique class and alias, and there will be only one of these view controllers active at a time, if that helps.

I'm probably missing something really easy. Any assistance would be welcome.


回答1:


I am not quite sure about your question. Here is what I understand: You have different views and there is no direct relation between them. But you want to reach each other view's controller. If so, please check this fiddle: https://fiddle.sencha.com/#fiddle/urg

Otherwise, I'd prefer you prepare a fiddle, which show your problem.

Ext.define('FirstPanel', {
            extend: 'Ext.panel.Panel',
            controller: 'FirstPanelController',
            layout: 'fit',
            itemId: 'firstPanel',
            items: [{
                xtype: 'button',
                text: 'FirstPanel Button',
                handler: 'FirstButtonClick'
            }]
        });
        Ext.define('FirstPanelController', {
            extend: 'Ext.app.ViewController',
            alias: 'controller.FirstPanelController',
            FirstButtonClick: function(button) {
                var me = this;
                var SecondPanelController = me.getView().mainView.down('#secondPanel').getController();
                SecondPanelController.FirstButtonClick();
                console.log('FirstButton Click');

            },
            SecondButtonClick: function() {
              alert('You are in FirstPanelController');  
            },
            init: function() {
                var me = this;     
            }
        });

        Ext.define('SecondPanelController', {
            extend: 'Ext.app.ViewController',
            alias: 'controller.SecondPanelController',
            SecondButtonClick: function(button) {
                var me = this;
                var FirstPanelController = me.getView().up().down('#firstPanel').getController();
                FirstPanelController.SecondButtonClick();
                console.log('SecondPanelController');

            },
            FirstButtonClick: function() {
                alert('You are in SecondPanelController');
            },
            init: function() {
                var me = this;     
            }
        });

        Ext.define('SecondPanel', {
            extend: 'Ext.panel.Panel',
            controller: 'SecondPanelController',
            layout: 'fit',
            itemId: 'secondPanel',
            items: [{
                xtype: 'button',
                text: 'SecondPanel Button',
                handler: 'SecondButtonClick'
            }]
        });


        Ext.define('MainPanel', {
            extend: 'Ext.panel.Panel',
            initComponent: function() {
              var me = this;
              me.items = [
                  Ext.create('FirstPanel', {
                      mainView: me
                  }),
                  Ext.create('SecondPanel')
              ]
              me.callParent();
            }
         });


来源:https://stackoverflow.com/questions/32934591/accessing-functions-in-viewcontroller-from-form-created-on-the-fly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!