Results 1 to 3 of 3

Thread: Add a static method to an existing class

  1. #1
    Sencha User Dumas's Avatar
    Join Date
    Dec 2008
    Location
    Vienna, Austria
    Posts
    589
    Answers
    5
    Vote Rating
    10
      0  

    Default Answered: Add a static method to an existing class

    Hello,

    I want to add a static method to an existing class. I tried with the code below, but is doesn't seem to have an effect.

    When I use the code without the statics property the method is initialized at Ext.Class.prototype.unregisterPreprocessor, but the it doesn't work to set it as static method under
    Ext.Class.unregisterPreprocessor.

    Code:
    /**
     * Add a unregisterPreprocessor method to the {@class Ext.Class}.
     */
     Ext.define('Bancha.loader.override.Class', {
        override: 'Ext.Class',
        statics: {
            /**
             * @private
             * @static
             * Unregister a preprocessor
             * 
             * @param {String} name The pre-processor's name.
             * @return {void}
             */
            unregisterPreprocessor: function(name) {
                // remove the processor
                delete this.getPreprocessors()[name];
                var preprocessors = this.getDefaultPreprocessors(),
                    position;
    
    
                // also remove from the stack (may be multipl times)
                while((position = preprocessors.indexOf(name)) !== -1) {
                    preprocessors = Ext.Array.splice(preprocessors, position, 1);
                }
    
    
                this.setDefaultPreprocessors(preprocessors);
            }
        }
    });
    What do I need to adopt?

  2. The reason why you can't use Ext.define to override Ext.Class is because Ext.Class isn't defined using Ext.define. However, you can use Ext.define in another way so you still get class loading and such:

    Code:
    Ext.define('Override.Class', {
    }, function() {
        Override.Class = null;
    
        Ext.Class.foobar = function() {
            console.log('foobar');
        };
    });
    You could make this more elegant but now you can execute Ext.Class.foobar().

    Just for coverage, you can use Ext.define to override a class to add statics. Here is a quick test I made:

    Code:
    Ext.define('MyClass', {
        statics : {
            foo : function() {
                console.log('foo');
            }
        }
    });
    
    Ext.define('Override.MyClass', {
        override : 'MyClass',
    
        statics : {
            bar : function() {
                console.log('bar');
            }
        }
    });
    
    MyClass.foo();
    
    MyClass.bar();

  3. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,553
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    The reason why you can't use Ext.define to override Ext.Class is because Ext.Class isn't defined using Ext.define. However, you can use Ext.define in another way so you still get class loading and such:

    Code:
    Ext.define('Override.Class', {
    }, function() {
        Override.Class = null;
    
        Ext.Class.foobar = function() {
            console.log('foobar');
        };
    });
    You could make this more elegant but now you can execute Ext.Class.foobar().

    Just for coverage, you can use Ext.define to override a class to add statics. Here is a quick test I made:

    Code:
    Ext.define('MyClass', {
        statics : {
            foo : function() {
                console.log('foo');
            }
        }
    });
    
    Ext.define('Override.MyClass', {
        override : 'MyClass',
    
        statics : {
            bar : function() {
                console.log('bar');
            }
        }
    });
    
    MyClass.foo();
    
    MyClass.bar();
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  4. #3
    Sencha User Dumas's Avatar
    Join Date
    Dec 2008
    Location
    Vienna, Austria
    Posts
    589
    Answers
    5
    Vote Rating
    10
      0  

    Default

    Hello Mitchell,

    thanks for the answer. I already solved the problem by doing:
    Code:
    Ext.define('Bancha.Loader', {
         requires: 'Ext.Loader'
    }, function() {
        Bancha.Loader = {};
    
    
        // Ext.Loader is a singleton,
        // so we need to directly apply the overrides
        Ext.apply(Ext.Loader, {
            ...
        });
    });
    Best regards,
    Roland

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •