Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my ExtJS singleton not working?

I have an ExtJS singleton class.

As a test I am calling a method of it in app.js launch() function.

But the singleton static method is not defined.

I thought when I require the class the singleton becomes active?

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);
                }
            }
        }
    }
});
like image 806
Greg Lafrance Avatar asked Feb 06 '14 07:02

Greg Lafrance


1 Answers

In Ext JS you can define class as singleton or define normal class with statics methods. You can not define statics methods in singleton.

If you define class as singleton Ext JS class post-processor immediately creates an instance of that class, and in your case store the reference in AM.localization.ResourceManager. Then you can access singleton methods like AM.localization.ResourceManager.initBundleLoader()

Good explenation of difference between normal class with statics methods and singleton you can find in third post here: http://www.sencha.com/forum/showthread.php?128646-Singleton-vs-class-with-all-static-members

So your class definition should be:

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);
            }
        }
    }
});
like image 71
Akatum Avatar answered Nov 15 '22 09:11

Akatum