Search code examples
javascriptextjsoverridingstatesuperclass

Overriding getState in ExtJS


I'm making a custom stateProvider and I want to save some extra stuff for my panels' state. I read that in order to do that, what you have to do is override the getState() function of your panel class but make sure to call the superclass method. I tried something like this:

initComponent: function () {
    Ext.apply(this, {
        getState: function() {
            this.superclass.getState.call();
            return {
                col: this.up().id,
                row: this.row,
                hidden: this.isHidden()
            };
        }
    });

    this.callParent(arguments)
}

but it doesn't seem to work. I'm confused as to how I must apply the superclass method...

The docs say the following about a component's getState() method:

Subclasses which implement more complex state should call the superclass's implementation, and apply their state to the result if this basic state is to be saved.

Any ideas where I'm wrong?


Solution

    1. override getState directly on your Panel definition
    2. fetch the result of the super method call and apply your changes

      Ext.define('MyPanel', {
          ...
          getState: function() {
              var result = this.callParent(arguments);
              Ext.apply(result, {
                  col: this.up().id,
                  row: this.row,
                  hidden: this.isHidden()
              });
              return result;
          }
      })