Results 1 to 7 of 7

Thread: Ext.define instances extraParams being set on all subsequent instances

  1. #1
    Sencha User
    Join Date
    Sep 2010
    Posts
    51
    Answers
    1
    Vote Rating
    3
      0  

    Default Answered: Ext.define instances extraParams being set on all subsequent instances

    If I define a Store definition, such as:

    Code:
    Ext.define('ContactStore', {    extend: 'Ext.data.Store',
        nocache:true,
        model : 'CRM_CONTACT',
        proxy : {
            type : 'ajax',
            url : 'index.php?option=com_foo',
            reader : {
                type : 'json',
                root : 'rows'
            },
            extraParams : {
                table : 'CRM_CONTACT_VIEW',
                order : 'LASTNAME',
                controller : 'Read'
            }
        },
        remoteSort:true,
        
        constructor: function() {
            this.callParent(arguments);
        }
    });
    and then instantiate it, and add extra parameters to the *instance's* proxy:

    Code:
     
    myStore = new ContactStore();
    myStore.getProxy().extraParams.RESELLER_ID =  10205;
    then *every* instance of ContactStore I create thereafter has the extra parameters set the same way.
    Why would this be the case when I am defining extra parameters on the instance and not the definition?
    Am I thinking about all this in too much of an Object Oriented way?

  2. The problem here is by sharing objects on class prototype. In your case, config value for proxy property is an object and this object is shared on prototype of ContactStore. So, all instances from ContactStore will share this object. To fix this, try to move proxy config into class constructor, like below:
    Code:
        Ext.define('ContactStore', {    
            extend: 'Ext.data.Store',
            nocache:true,
            model : 'CRM_CONTACT',
    //      proxy : {
    //          type : 'ajax',
    //          url : 'index.php?option=com_foo',
    //          reader : {
    //              type : 'json',
    //              root : 'rows'
    //          },
    //          extraParams : {
    //              table : 'CRM_CONTACT_VIEW',
    //              order : 'LASTNAME',
    //              controller : 'Read'
    //          }
    //      },
            remoteSort:true,
            
            constructor: function() {
                var me = this;
                me.proxy = {
                    type : 'ajax',
                    url : 'index.php?option=com_foo',
                    reader : {
                        type : 'json',
                        root : 'rows'
                    },
                    extraParams : {
                        table : 'CRM_CONTACT_VIEW',
                        order : 'LASTNAME',
                        controller : 'Read'
                    }
                };
                me.callParent(arguments);
            }
        });

  3. #2
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,409
    Answers
    716
    Vote Rating
    504
      0  

    Default

    Are you using MVC pattern? If so .. continue using define and it will be instantiated automatically. If you are not, just create the store.. not both.

    You can use just use: extraParams : {}; // clear previous

    Scott.

  4. #3
    Sencha User
    Join Date
    Sep 2010
    Posts
    51
    Answers
    1
    Vote Rating
    3
      0  

    Default

    Thanks for your reply. Can you elaborate just a little on the meaning of 'continue using define' .. I guess you are referring to a known MVC methodology for using Ext - using Define for *all* instantiation of JS Objects?
    If you could confirm that or point me to a resource it'd really help.
    Thanks again,

  5. #4
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,409
    Answers
    716
    Vote Rating
    504
      0  

    Default

    If you could confirm that
    Correct ..

    When using MVC, it will automatically create the instance as needed.

    Scott.

  6. #5
    Sencha User
    Join Date
    Jan 2012
    Posts
    1,376
    Answers
    347
    Vote Rating
    119
      0  

    Default

    The problem here is by sharing objects on class prototype. In your case, config value for proxy property is an object and this object is shared on prototype of ContactStore. So, all instances from ContactStore will share this object. To fix this, try to move proxy config into class constructor, like below:
    Code:
        Ext.define('ContactStore', {    
            extend: 'Ext.data.Store',
            nocache:true,
            model : 'CRM_CONTACT',
    //      proxy : {
    //          type : 'ajax',
    //          url : 'index.php?option=com_foo',
    //          reader : {
    //              type : 'json',
    //              root : 'rows'
    //          },
    //          extraParams : {
    //              table : 'CRM_CONTACT_VIEW',
    //              order : 'LASTNAME',
    //              controller : 'Read'
    //          }
    //      },
            remoteSort:true,
            
            constructor: function() {
                var me = this;
                me.proxy = {
                    type : 'ajax',
                    url : 'index.php?option=com_foo',
                    reader : {
                        type : 'json',
                        root : 'rows'
                    },
                    extraParams : {
                        table : 'CRM_CONTACT_VIEW',
                        order : 'LASTNAME',
                        controller : 'Read'
                    }
                };
                me.callParent(arguments);
            }
        });

  7. #6
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,409
    Answers
    716
    Vote Rating
    504
      0  

    Default

    The problem here is by sharing objects on class prototype
    Yes sir .. i just plain missed that one .. thanks

    Scott.

  8. #7
    Sencha User
    Join Date
    Sep 2009
    Location
    Providence, RI
    Posts
    5
    Vote Rating
    0
      0  

    Default shared extraParams object between 2 different proxy instances

    I think the problem is really with the Ext.data.proxy.Server constructor function. I ran into this issue today myself and the issue is not that the proxies are shared but that the extraParams object is shared across all instances of that proxy. The proxies themselves are fine and can be left in the class definition.

    What should change is how Ext.data.proxy.Server sets its extraParams property. Rather than pointing to the extraParams object in the config, it should make a unique copy.

    Ext.data.proxy.Server
    Code:
        constructor: function(config) {
            var me = this;
    
    
            config = config || {};
            /**
             * @event exception
             * Fires when the server returns an exception
             * @param {Ext.data.proxy.Proxy} this
             * @param {Object} response The response from the AJAX request
             * @param {Ext.data.Operation} operation The operation that triggered request
             */
            me.callParent([config]);
    
    
            /**
             * @cfg {Object} extraParams
             * Extra parameters that will be included on every request. Individual requests with params of the same name
             * will override these params when they are in conflict.
             */
            me.extraParams = config.extraParams || {};
    
    
            me.api = Ext.apply({}, config.api || me.api);
            
            //backwards compatibility, will be deprecated in 5.0
            me.nocache = me.noCache;
        }
    Code:
    me.extraParams = config.extraParams || {};
    should really be
    Code:
    me.extraParams = config.extraParams ? Ext.clone(config.extraParams) : {};
    Tim Eagan
    Modus Create

Tags for this Thread

Posting Permissions

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