Javascript ExtJs4 - 存储 baseParams 配置属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6060947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:04:50  来源:igfitidea点击:

ExtJs4 - Store baseParams config property?

javascriptextjsstoreparamsextjs4

提问by shane87

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

在 extjs3.x 中,我使用商店baseParams配置属性来指定用于加载商店的参数。
这个属性在 extjs 4 中不再存在。我应该怎么做而不是这个?

Also in extjs3 I was able to specify wether the stores proxy was a GETor a POSTmethod by using the proxy methodconfig property. What should I do instead of this?

同样在 extjs3 中,我能够通过使用代理配置属性来指定商店代理是一​​种GET还是一种POST方法method。我应该怎么做而不是这个?

My ExtJs 3 code ->

我的 ExtJs 3 代码 ->

   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

回答by Craig

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:

您需要使用“extraParams”代理属性来代替 Ext 3 中的 baseParams 代理属性。 ExtJS 4 中的等效 JsonStore 如下所示:

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.

据我所知,HTTP 传输方法是根据 RESTful 原则根据您要完成的任务自动设置的。例如,如果您加载商店,则使用 GET 请求;创建新记录使用 POST 等。

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

如有必要,您可以通过覆盖代理的 actionMethods 属性来覆盖它:

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

回答by korgoth

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]);
    }
});

回答by Daniel

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}});