ExtJs4 - Store baseParams config property?
Asked Answered
U

3

16

In extjs3.x I used the stores baseParams config property to specify parameters used to load the store.
This property no longer exists in extjs 4. What should I do instead of this?

Also in extjs3 I was able to specify wether the stores proxy was a GET or a POST method by using the proxy method config property. What should I do instead of this?

My ExtJs 3 code ->

   var store = new Ext.data.JsonStore({
        root: 'Data',
        baseParams: {
           StartDate: '',
           EndDate: '''
        },//baseParams
    proxy: new Ext.data.HttpProxy({
        url: 'Time/Timesheet',
        method: 'POST'
    })//proxy
});//new Ext.data.JsonStore
Unrivalled answered 19/5, 2011 at 15:19 Comment(2)
"What should I do instead of this?" Find a new framework. It's unbelievable to me that Sencha so dramatically changed things from ExtJS 3 to 4Ally
@George: yeah it was quite a big change. I have been migrating my project for the last month. Nearly there. i think the benefits will pay off once fully migrated. You having trouble with it?Unrivalled
W
29

You need to use the 'extraParams' proxy property in place of the baseParams one from Ext 3. An equivalent JsonStore in ExtJS 4 looks like this:

Ext.define('YourModel', {
    extend: 'Ext.data.Model',
    fields: ['field1', 'field2']
}); 

var store = new Ext.data.Store({
    model: 'YourModel',
    proxy: {
        type: 'ajax',
        url : 'Time/Timesheet',
        root: 'Data',
        extraParams: {
            StartDate: '',
            EndDate: ''
        }
    }
});

As far as I know, the HTTP transport method is set automatically according to RESTful principles according to what you're trying to accomplish. For example, if you load the store a GET request is used; creating a new record uses a POST, etc.

You can override this if necessary though, by overriding the actionMethods property of the proxy:

var store = new Ext.data.Store({
    model: 'YourModel',
    proxy: {
        type: 'ajax',
        url : 'Time/Timesheet',
        root: 'Data',
        actionMethods: {
            read: 'POST'
        },
        extraParams: {
            StartDate: '',
            EndDate: ''
        }
    }
});
Whichsoever answered 19/5, 2011 at 15:50 Comment(2)
Thanks for your response. I caould see that from the API about the automatic setting of the HTTP transport method. I need a way to override this as I want to use a POST request to load my store as the GET request is restricted in length. Any ideas? and Thanks again.Unrivalled
@Unrivalled Yep, did some digging through the docs and looks like there's a way to override it. I've updated the answer.Whichsoever
R
8

Problem with proxy extra params: proxy is common to all stores created with this proxy! Example:

var countries1, countries2;
countries1 = Ext.create("MyApp.store.Countries");
countries1.getProxy().setExtraParam("countriesId", 1) // add parameter 'countriesId' = 1
countries1.load();
countries2 = Ext.create("MyApp.store.Countries");
countries2.getProxy().setExtraParam("countriesId", 2) // add parameter 'countriesId' = 2
countries2.load({
    callback: function(){
        countries1.load(); // 'countriesId' is no more 1, but 2 !!!
    }
});    

I advise you to create your own store class which implements base parameters if you want to set parameters before calling 'load' function with several stores of the same type.

Ext.define("MyStore",{
    extend: "Ext.data.Store",
    baseParams: null,
    /**
     * add base parameter to store.baseParams
     * @param {Object} key/value object: {key: value}
     */
    addBaseParam: function(obj){
        Ext.apply(this.baseParams, obj);
    },
    /**
     * add several base parameters to store.baseParams
     * @param {Array}: array of key/value object: [{key1: value1, key2: value2,...}]
     */
    addBaseParams: function(objects){
        var me = this;
        if (Ext.isArray(objects)){
            Ext.each(objects, function(obj){
                me.addBaseParam(obj);
            })
        } else if (objects){
            me.addBaseParam(objects);
        }
    },
    /**
     * reset base parameters
     */
    resetBaseParams: function(){
        this.baseParams = {};
    },
    /**
     * constructor
     * @param {object} config
     */
    constructor: function(config) {
        var me = this;
        // manage base params
        me.baseParams = me.baseParams || {};
        // call parent
        me.callParent(arguments);
    },
    /**
     * override load method to add base params to request params
     * @param {Object} options
     */
    load: function(options){
        var me = this;
        options = options || {};
        options.params = options.params || {};
        Ext.applyIf(options.params, me.baseParams);
        me.callParent([options]);
    }
});
Retinitis answered 30/12, 2012 at 12:43 Comment(0)
O
1
var DataStore = new Ext.data.Store({
model: 'TestItem',
id: 'DataStore',
 proxy: {
   type: 'ajax',
   url : 'db_categoria.php',            
   actionMethods: {
           read: 'POST'
       },
   extraParams: {
            task: 'LISTING',
       },
   reader: {
    root: 'results',
    totalProperty: 'total',
      id: 'id'
     }
    }
});
DataStore.load({params: {start: 0, limit: 20}});
Oleic answered 18/10, 2011 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.