Use and function of the new Ext.app.EventDomain

痞子三分冷 提交于 2019-12-06 00:16:51

Ext.app.EventDomain is not intended to be used per se; rather, you can implement custom event domains if you need them. The idea behind event domains is quite simple: it's a way to pass events between application parts that happen not to be Ext.Component-derived.

The most frequent use is Controller binding: in 4.1 it was only possible to call other Controllers' methods directly (hard binding), which is very bad for testing. In 4.2 you can have a Controller listen to other Controllers' events instead (soft binding) and have clear logic separation, so instead of:

Ext.define('MyApp.controller.Foo', {
    extend: 'Ext.app.Controller',

    doSomething: function() {
        this.getController('Bar').doSomethingElse();
    }
});

Ext.define('MyApp.controller.Bar', {
    extend: 'Ext.app.Controller',

    doSomethingElse: function() {
        // The problem here is that this logic belongs to Bar controller
        // but this method has to be called from Foo controller,
        // which means Bar should always be around whenever Foo
        // needs to call it. Race conditions, anyone?
        ...
    }
});

You can do:

Ext.define('MyApp.controller.Foo', {
    extend: 'Ext.app.Controller',

    doSomething: function() {
        this.fireEvent('doSomethingElse');
    }
});

Ext.define('MyApp.controller.Bar', {
    extend: 'Ext.app.Controller',

    init: function() {
        this.listen({
            controller: {
                '*': {        // '*' means any controller
                    doSomethingElse: this.doSomethingElse
                }
            }
        });
    },

    doSomethingElse: function() {
        // Not a problem anymore -- Foo fires an event, and if Bar
        // happens to be around, it reacts and does whatever it wants;
        // the benefit is that there is no direct method calling
        // so we don't have to watch for exceptions and do other
        // unnecessary stuff.
        ...
    }
});

It's also ok to listen to your own events -- which is not something you'd probably use in production but is a nice side effect that can be used very successfully for controller unit testing.

Besides other Controllers' events, a Controller can now listen to Store, Direct provider and global events, which may be useful at times.

I plan to write up on this when 4.2 is released; hope this helps a bit until then.

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