1. #1
    Sencha User
    Join Date
    Jun 2009
    Posts
    60
    Vote Rating
    0
    kchiu is on a distinguished road

      0  

    Default Best practice for custom component properties?

    Best practice for custom component properties?


    Hi there,

    What's the suggested location for storing extra properties for custom components defined via Ext.define()? Where should we store these extra properties without clashing with any existing / future properties of the Component we're extending?

    eg. given the code below, where should I store the 'map' property used by redrawMap()? And for that matter, should redrawMap() be nested under a property specifically for holding all other custom properties?

    cheers.

    Code:
       Ext.define('VVG.view.MapView', {
          alias : 'widget.mapview',
          extend: 'Ext.container.Container',
          requires: ['Ext.container.Container']
          
          ,afterComponentLayout : function() {
             this.redrawMap();
             this.callParent(arguments);
          }
          
          ,redrawMap: function() {
             var map = this.map;
             if(!map) {
                this.map = vvg.lib.createMap(this.getId());
             } else {
                // probably redundant
                map.render(this.getId());
    
    
                // required
                for (var i = 0; i < map.layers.length; i++) {
                   if(map.layers[i].getVisibility() === true)
                      map.layers[i].redraw();            
                }
             }
          }
       });

  2. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,496
    Vote Rating
    283
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    Storing them directly on this is fine. Yes, there's a risk your properties could clash with the superclass but in practice it happens very rarely and it isn't worth throwing in extra complexity to avoid it. Generally I try to stay clear of generic property names like map to reduce the chances of a clash but that's about it.

    The most common source of problems along these lines is event handlers. I tend to put the suffix Handler on mine to make sure I don't clash with the library's template methods.

Thread Participants: 1