如何在EXTJS中使用全局函数/实用程序类 [英] How to use global functions/Utility Class in EXTJS

查看:249
本文介绍了如何在EXTJS中使用全局函数/实用程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码结构如下->

My code structure is as follows->

MyApp->应用程序->控制器,模型,存储,共享(util-> Utility.js),视图

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;
    }
});

我正在尝试在myGrid中渲染列时调用myFunction,如下所示->

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.

推荐答案

更好的方法是将类声明为单例:

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);
    }        
});

这篇关于如何在EXTJS中使用全局函数/实用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆