【问题标题】:Why is my ExtJS singleton not working?为什么我的 ExtJS 单例不起作用?
【发布时间】:2014-02-06 07:40:14
【问题描述】:

我有一个 ExtJS 单例类。

作为测试,我在 app.js 的 launch() 函数中调用它的一个方法。

但是没有定义单例静态方法。

我认为当我需要课程时,单身人士会变得活跃?

Ext.Loader.setConfig({
    enabled : true,
    paths: {
        'AM': 'app'
    }
});


Ext.application({
    name: 'AM',
    autoCreateViewport: true,


    requires: [
        'AM.localization.ResourceManager'
    ],


    controllers: [
        'Users'
    ],


    launch: function() {
        alert(ResourceManager.initBundleLoader());
    }
});




Ext.define('AM.localization.ResourceManager', {
    alternateClassName: 'ResourceManager',
    singleton: true,

    init: function() {
        this.initBundleLoader();
    },

    statics: {
        test: 'here',
        initBundleLoader: function() {
            debugger;
            Ext.applyIf(Ext.Loader, {
                resourceBundles: new Object()
            });
        },

        registerBundle: function(bundleName, locale) {
            debugger;
            if(!Ext.Loader.hasOwnProperty('resourceBundles')) {
                this.initBundleLoader();
            }
            if(!Ext.Loader.resourceBundles.hasOwnProperty(bundleName)) {
                if(Ext.ClassManager.isCreated('AM.locale.' + locale + '.resources.' + bundleName)) {
                    this.resourceBundles.bundleName = Ext.create('AM.locale.' + locale + '.resources.' + bundleName);
                }
            }
        }
    }
});

【问题讨论】:

    标签: extjs singleton extjs4.2


    【解决方案1】:

    在 Ext JS 中,您可以将类定义为 singleton 或使用 statics 方法定义普通类。你不能在单例中定义静态方法。

    如果您将类定义为singleton,Ext JS 类后处理器会立即创建该类的实例,并在您的情况下将引用存储在AM.localization.ResourceManager 中。然后你可以访问像AM.localization.ResourceManager.initBundleLoader()这样的单例方法

    普通类与静态方法和单例之间的区别很好的解释,你可以在第三篇文章中找到:http://www.sencha.com/forum/showthread.php?128646-Singleton-vs-class-with-all-static-members

    所以你的类定义应该是:

    Ext.define('AM.localization.ResourceManager', {
        alternateClassName: 'ResourceManager',
        singleton: true,
    
        init: function() {
            this.initBundleLoader();
        },
    
        test: 'here',
        initBundleLoader: function() {
            debugger;
            Ext.applyIf(Ext.Loader, {
                resourceBundles: new Object()
            });
        },
    
        registerBundle: function(bundleName, locale) {
            debugger;
            if(!Ext.Loader.hasOwnProperty('resourceBundles')) {
                this.initBundleLoader();
            }
            if(!Ext.Loader.resourceBundles.hasOwnProperty(bundleName)) {
                if(Ext.ClassManager.isCreated('AM.locale.' + locale + '.resources.' + bundleName)) {
                    this.resourceBundles.bundleName = Ext.create('AM.locale.' + locale + '.resources.' + bundleName);
                }
            }
        }
    });
    

    【讨论】:

    • 现在很好用。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2014-10-28
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    相关资源
    最近更新 更多