Where should I define global functions in ExtJS 4 MVC?

Julian Hollmann picture Julian Hollmann · Feb 13, 2012 · Viewed 14.1k times · Source

I need to define some functions which i can call everywhere in my app. What is the best approach to achieve this?

Answer

David Kanarek picture David Kanarek · Feb 14, 2012

Personally, to keep it as EXT-MVC as possible, I have a Utilities class full of static methods. This can be required like any other class to maintain proper dependency relations. This also ensures that the methods are run in an EXT environment, so all of EXT is available.

Ext.define('MyApp.Utilities', {
    statics: {
        foo: function (a, b) {
            return a + b;
        }
    }
});

Ext.define('MyApp.MyView', {
    extends: 'Ext.panel.Panel',
    requires: ['MyApp.Utilities'],

    initComponent: function () {
        MyApp.Utilities.foo(1, 2);
    }
});