I have the following two classes which extend the ExtJs Grid panel:
- ReadonlyDataGrid - as name suggests, basic non-editable ExtJs grid
- EditableDataGrid - ExtJs grid with cell & row editing plugin
Now I want to provide a new class which will extend one of the above 2 classes based on a property set by the developer in the grid JSON configuration
For Eg:
Java:
parentObj.put("gridType", "readonly");
JS:
Ext.define('Grid.view.GridFactory', {
extend: 'Grid.view.AbstractGridFactory',
alias: 'widget.gridfactory',
singleton: true,
create : function(model){
    var grid;
    switch(model.input.gridType)
    {
        case 'readonly':
            grid = new Grid class extending the ReadonlyDataGrid class;
            break;
        case 'editable':
            grid = new Grid class extending the EditableDataGrid class;
            break;          
    }
    return grid;
  }
});
Right now I have to create two separate class files to handle this.
Class 1:
Ext.define('Grid.view.EditableGrid',{
extend : 'Grid.view.EditableDataGrid',
.... //additional features
});
Class 2:
Ext.define('Grid.view.ReadonlyGrid',{
extend : 'Grid.view.ReadOnlyDataGrid',
.... //additional features
});
- How can I merge these 2 classes into 1?
- How can I change the extend property or extend the correct class based on the user configuration as shown above?
- Design wise what is better? Having 1 class and changing the superClass at runtime or maintaining 2 classes but with redundant/same code?
 
                        
extendproperty you see fit:But that is bad practice.
Changing the superClass at runtime is even worse. Designwise, there are two seemly options:
editableand will only instantiate the relevant editing plugins / editors when the option istrue.