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

标签 javascript extjs sencha-touch

我有 2 个选择字段,其中包含通过 JSONP 代理从数据存储连接的选项。 我需要根据第一个选择字段中选择的值更改第二个选择字段中的值。为此,我认为我应该根据选择字段中选择的值更改代理 extraParams 中的参数。

我已经尝试过这篇文章 How can I change/add params on a store 中的 .getProxy() 方法但它不起作用。我在控制台中遇到错误:

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

请参阅下面的代码及其说明。 有什么想法如何做到这一点吗?

型号:

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

第一家商店:

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

第二家商店:

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

布局:

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'                            
    }

]

谢谢。

最佳答案

您可以创建一个新的存储实例并设置其代理,然后重置选择字段的存储。您可以将此代码添加到您的监听器函数中:

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;

关于javascript - Sencha Touch 2.0 动态更改数据存储 extraParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10288061/

相关文章:

javascript - 当文本区域实际上在视线范围内时,如何防止 IE10 自动滚动到焦点上的文本区域

javascript - 检测 AJAX 请求何时开始

extjs - 如何创建向 Controller 注册的模型的新实例?

html - 从 Sencha Touch Ext.data.JSONP.request 响应获取变量

jquery-mobile - 类似于 Sencha Touch 的覆盖层/对话框

java - 将数据从 JSON 文件导入 Sencha Touch 存储

javascript - XD 插件中的 npm 包或 Node.js API

javascript - Ext.tree.TreeNode设置tooltip动态

html - 如何通过sencha touch中动态创建的按钮处理特定事件

javascript - 如何使用 Javascript 遍历 Elements 子节点?