Results 1 to 6 of 6

Thread: How to pass a variable to extraParams of store

  1. #1
    Sencha User
    Join Date
    Jul 2011
    Posts
    72
    Answers
    2

    Default Answered: How to pass a variable to extraParams of store

    Hi everybody,

    I want to pass a variable to extraParams of store. But It doesn't work.

    Code:
    Ext.define('myModel', {
        extend: 'Ext.data.Model',
        
        fields: [
            {name: 'name', type: 'auto'},
            {name: 'value', type: 'auto'}
        ]
    });

    Code:
    Ext.define('myStore, {
        extend: 'Ext.data.Store',
        
        model: 'myModel,
                autoLoad: false,
        proxy: {
            type: 'ajax',
            url: 'xxx.php',
            reader: {
                type: 'json',
                root: 'root'
            },
            sortParam: null,
            pageParam: null,
            startParam: null,
            limitParam: null,     
            extraParams: {
                 param: this.myParam 
            }
        }
    });

    Code:
    Ext.define('myGrid', {extend: 'Ext.grid.Panel',    
            initComponent: function() {
                    this.store = Ext.create('myStore', {
                          myParam: 'something according to different condition'
                    });
      
                   this.columns = [......];
      
                   Ext.apply(this, {
                         title: 'My Grid'
                   });
                   this.callParent(arguments); 
            });
        }
    });
    Code:
    var grid = Ext.create('myGrid');
    grid.renderTo(document.body);

  2. Because the store gets the model in the constructor. You can still have the separation, what you need to do is just put your own configs in your store class.
    Code:
    Ext.define('myGeneicStore', {
        // ...
        myParam: '',
        constructor: function()
        {
             this.extraParams.myParam = this.myParam;
             this.callParent(arguments);
         }
    });
    
    // in code
    Ext.create('myGenericStore', {
        // ...
        model: 'myModel',
        myParam: 'something'
        
    });

  3. #2
    Sencha Premium Member
    Join Date
    May 2012
    Posts
    114
    Answers
    4

    Default

    I'm not sure because I never create stores this way but should it not be:

    Ext.define('myGrid', {
    extend: 'Ext.grid.Panel',
    initComponent: function() {
    this.store = Ext.create('myStore', {
    proxy : {
    extraParams : {
    myParam: 'something according to different condition'
    }
    }
    });
    this.columns = [......];
    Ext.apply(this, { title: 'My Grid' });
    this.callParent(arguments); }); } });
    Just a guess...

  4. #3
    Sencha User
    Join Date
    May 2012
    Location
    Blumenau - Brazil
    Posts
    50
    Answers
    12

    Default

    Code:
    extraParams: {
        myParam: this.myParam
    }
    It's a static code, so the this probably is referencing the window object and it's just evaluated when the class is instantiated, not when it's created.

    You should do something like
    Code:
    Ext.define('mystore', {
        extend: 'Ext.data.Store',
        constructor: function()
        {
             this.extraParams.myParam = this.myParam;
             this.callParent(arguments);
         }
    });
    --
    Jlio Csar Batista - blog - twitter
    Web Developer

  5. #4
    Sencha User
    Join Date
    Jul 2011
    Posts
    72
    Answers
    2

    Default

    It's do strange to create store like this way. We offen use Ext.create to create store in grid directly instead of Ext.define. But we should define store independant of grid in MVC model.

    So I want to know how to ...

  6. #5
    Sencha User
    Join Date
    Jul 2011
    Posts
    72
    Answers
    2

    Default

    This code is OK now.

    Code:
    Ext.define('myStore', {
        extend: 'Ext.data.Store',
     constructor: function(config) {
          this.autoLoad = true;
          this.model = 'myModel';
      this.proxy = {
       type: 'ajax',
       url: 'xxx.php',
       reader: {
        type: 'json',
        root: 'root'
       },
       extraParams: {
              myParam: config.myParam
       }
      };
      this.callParent(arguments);
    })
    But why firebug show me.model is undefined if I used initComponent instead of constructor?

  7. #6
    Sencha User
    Join Date
    May 2012
    Location
    Blumenau - Brazil
    Posts
    50
    Answers
    12

    Default

    Because the store gets the model in the constructor. You can still have the separation, what you need to do is just put your own configs in your store class.
    Code:
    Ext.define('myGeneicStore', {
        // ...
        myParam: '',
        constructor: function()
        {
             this.extraParams.myParam = this.myParam;
             this.callParent(arguments);
         }
    });
    
    // in code
    Ext.create('myGenericStore', {
        // ...
        model: 'myModel',
        myParam: 'something'
        
    });
    --
    Jlio Csar Batista - blog - twitter
    Web Developer

Posting Permissions

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