简体   繁体   English

Sencha Touch 2.0动态更改数据存储extraParams

[英]Sencha Touch 2.0 dynamicly change data store extraParams

I have 2 selectfields with options connected from data store with JSONP proxy. 我有2个选择字段,其中的选项从数据存储与JSONP代理连接。 I need change value in 2nd selectfield based on value selected in first selectfield. 我需要根据第一个选择字段中选择的值在第二个选择字段中更改值。 For this I think I should change parameters in proxy extraParams based on value selected in selectfield. 为此我想我应该根据selectfield中选择的值更改代理extraParams中的参数。

I've tried .getProxy() method from this post How can I change/add params on a store but it's does not work. 我在这篇文章中尝试过.getProxy()方法如何在商店中更改/添加params但是它不起作用。 I have an error in console: 我在控制台中有错误:

Uncaught TypeError: Object function () {
                    return this.constructor.apply(this, arguments);
                } has no method 'getProxy'

See code below with explanations. 请参阅下面的代码并附上说明。 Any ideas how to do that? 任何想法如何做到这一点?

Model: 模型:

Ext.define('Providers.model.Provider', {    
extend: 'Ext.data.Model',    
config: {       
   fields: [
        {
            name: 'id',
            type: 'int'
        }, 
        {
            name: 'name',
            type: 'string'
        }
    ]
}
});

1st Store: 第一店:

Ext.define('Providers.store.ProvidersType', {    
extend: 'Ext.data.Store',
config: {
    model: 'Providers.model.Provider',
    proxy: {
        type: 'scripttag',
        url : 'http://example.com/providers/service.php',
        extraParams: {
            action: 'provider_types',
            username: 'test2',
            callback: '?',                      
            format: 'json'          
        },          
        reader: {
            type: 'json',
            rootProperty: 'providers'
        }
    },      
    autoLoad: true
}
});

2nd store: 第二家店:

Ext.define('Providers.store.Countries', {    
extend: 'Ext.data.Store',
config: {
    model: 'Providers.model.Provider',
    proxy: {
        type: 'scripttag',
        url : 'http://example.com/providers/service.php',
        extraParams: {
            action: 'countries',
            username: 'test2',
            provider_type: 14, //<--- here should be value from first selectfield
            callback: '?',                      
            format: 'json'          
        },          
        reader: {
            type: 'json',
            rootProperty: 'countries'
        },          
    },
    autoLoad: true      
}
});

Layout: 布局:

xtype: 'fieldset',
layout: 'vbox',                     
items: [                        
    {
        xtype: 'selectfield',
        id: 'selectType',           
        cls: 'combobox',
        store: 'ProvidersType',
        displayField: 'name',
        valueField: 'id',                           
        listeners: {
            change: function(field, value) {                                    
                if (value instanceof Ext.data.Model) {
                    value = value.get(field.getValueField());
                }
                console.log(value); //<-- get value works fine, how insert it in proxy param? 
                //Providers.store.Countries.getProxy().extraParams.provider_type = 10; <-- Do not work

            }
        }                           
    },                          
    {
        xtype: 'selectfield',   
        id: 'selectCountry',
        placeHolder: 'Country',
        cls: 'combobox',                        
        store: 'Countries',
        displayField: 'name',
        valueField: 'id'                            
    }

]

Thanks. 谢谢。

You can create a new store instance and set its proxy and then reset store of the select field. 您可以创建新的商店实例并设置其代理,然后重置选择字段的存储。 You can add this code to your listener function: 您可以将此代码添加到侦听器函数:

newstore = Ext.create('Providers.store.Countries', {});
newstore.setProxy(
    {
        type: 'scripttag',
        url : 'http://example.com/providers/service.php',
        extraParams: {
            action: 'countries',
            username: 'test2',
            provider_type: value,
            callback: '?',                      
            format: 'json'          
        },          
        reader: {
            type: 'json',
            rootProperty: 'countries'
        },          
    }
);
newstore.load();

countrySelection = Ext.getCmp('selectCountry');
countrySelection.setStore(newstore); 
//OR: countrySelection.store = newstore;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM