Where should I define global functions in ExtJS 4 MVC?

前端 未结 2 926
青春惊慌失措
青春惊慌失措 2020-12-15 04:05

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

2条回答
  •  旧巷少年郎
    2020-12-15 04:53

    An alternative way other than @David Kanarek's statics approach is to define a singleton. Codes:

    Ext.define('MyApp.Utilities2', {
        singleton: true,
        global_var2: 'Hello World',
        foo2: function (a, b) {
            return a + b;
        },
    });
    

    I've created a fiddle here: https://fiddle.sencha.com/#fiddle/qu1

    The difference between the statics and singleton approach is that

    • MyApp.Utilities2 (singleton approach) is an object,
    • MyApp.Utilities (statics approach) is a class.

    So It's up to you whether to reference the class itself or to reference one instance of that class for convenience.

提交回复
热议问题