Search code examples
extjssencha-touchtouchextend

Should I extend Ext.app.Application class in Sencha Touch 2?


Should I extend Ext.app.Application class in Sencha Touch 2? I have functionality which is common for several applications, for example: do something for all stores configured in the application. If yes, how do I start the application in app.js?


Solution

  • I will answer for ExtJS 4.0.x / 4.1.x and SenchaTouch 2.0.x / 2.1.x

    You should not extend Ext.app.Application

    Why?

    You only can only have one active application at a time which should (need to be) always be configured & startet by calling Ext.application({...});.

    As always there are ways around this (in both frameworks) which would then allow you to use your own instance. But would not recommend it!

    What would be needed?

    Basically the only thing that is required is to reimplement and extend the code within the Ext.application method so that it is capable to handle any other class then Ext.app.Application. I will just add a SenchaTouch example, for ExtJS see ExtJS4.2 section below the code.

    SenchaTouch example

    application: function(appClass,config) {
        var appName = config.name,
            onReady, scope, requires;
    
        if (!config) {
            config = {};
        }
    
        if (!Ext.Loader.config.paths[appName]) {
            Ext.Loader.setPath(appName, config.appFolder || 'app');
        }
    
        requires = Ext.Array.from(config.requires);
        config.requires = [appClass];
    
        onReady = config.onReady;
        scope = config.scope;
    
        config.onReady = function() {
            config.requires = requires;
            Ext.create(appClass, config);
    
            if (onReady) {
                onReady.call(scope);
            }
        };
    
        Ext.setup(config);
    },
    

    A note to ExtJS 4.2

    Sencha has announced ExtJS 4.2 which will allow us to extend from the Ext.app.Application class