How to use global functions/Utility Class in EXTJS

陌路散爱 提交于 2019-12-11 03:25:46

问题


My code structure is as follows->

MyApp-> app -> controller, model, store, shared (util-> Utility.js), view

I have created the following Utility Class

Ext.define('MyApp.shared.util.Utilities', {

    myFunction : function (val, meta, rec) {
        //do something 
        return val;
    }
});

I am trying to call the myFunction while rendering a column in my grid as follows ->

Ext.define('MyApp.view.MyGrid', {
        extend: 'Ext.grid.Panel',        

        initComponent: function() {
            var rendererFn = Ext.create('MyApp.shared.util.Utilities');

            this.columns = [{
                text: 'Col1',
                dataIndex: 'col1'
                renderer: rendererFn.myFunction
            };

            this.store = new MyApp.store.MyStore();
            this.store.load();
            this.callParent(arguments);
        }        
});

This works fine but I want to know if this is the correct way of using global functions.


回答1:


A better way would be to declare your class a singleton:

Ext.define('MyApp.shared.util.Utilities', {
    singleton: true,
    myFunction : function (val, meta, rec) {
        //do something 
        return val;
    }
});

Ext.define('MyApp.view.MyGrid', {
    extend: 'Ext.grid.Panel',        

    initComponent: function() {
        this.columns = [{
            text: 'Col1',
            dataIndex: 'col1'
            renderer: MyApp.shared.util.Utilities.myFunction
        }];

        this.store = new MyApp.store.MyStore();
        this.store.load();
        this.callParent(arguments);
    }        
});


来源:https://stackoverflow.com/questions/30516310/how-to-use-global-functions-utility-class-in-extjs

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