简体   繁体   English

ExtJS:如何在另一个函数中调用一个函数?

[英]ExtJS: How to call a function within another function?

I know there are a bunch of questions about calling a function in another function but unfortunately none of them worked for me or I couldn't implement to my project! 我知道有很多关于在另一个函数中调用一个函数的问题,但是不幸的是,这些问题都不对我有用 ,或者我无法实现我的项目!

I've a Window class and two functions have been stated inside. 我有一个Window类,并在其中声明了两个函数。 The second one is getting getting a specific URL with ID through a combobox and returning it. 第二个是通过combobox获取具有ID的特定URL并将其返回。 I need to use this generated URL in the first function ; 我需要在第一个function使用生成的URL

getSelectCountry: function(){
        var countryRecord   = Ext.ComponentQuery.query('[name=country]')[0].getSelectedRecord();
        var countryId       = countryRecord.get('id');
        var urlWithId       = MyApp.Globals.getUrl() + '/city/view/list/?countryid='+ countryId;

        // var store =  Ext.getStore('cityCombo');
        // store.load({
        //     url : MyApp.Globals.getUrl() + '/city/view/list/?countryid='+ countryId,
        // });

        return urlWithId;
    }

and I need to use this returned urlWithId below in citycombo , within proxy 's url . 我需要在下面的citycombo中在proxyurl使用此返回的urlWithId It'll list cities depends on country that below; 下面将列出城市取决于国家/地区;

Ext.define('MyApp.view.weather.SettingsWindow', { 
...
getSettingsItems: function () {
        var me = this;

        var settingsItems = [
           {
                xtype: 'form',
                layout: {type: 'vbox', align: 'stretch', pack: 'start'},
                reference: 'settingsForm',
                items: [
{
                        xtype: 'citycombo',
                        itemId: 'cityName',
                        name: 'city',
                        afterLabelTextTpl: MyApp.Globals.required,
                        allowBlank: false,
                        flex: 1,
                        store: {
                            storeId: 'cityCombo',
                            proxy: {
                                type: 'ajax',
                                // I neeed returned URL with ID on here.
                                url: MyApp.Globals.getUrl() + '/city/view/list/', 
                                reader: {
                                    type: 'json',
                                    rootProperty: 'data'
                                }
                            },
                            pageSize: 0,
                            sorters: 'description',
                            autoLoad: true
                        }
                    },

I've tried use store.load or create a new variable and calling it but I couldn't be success for any of tries. 我试过使用store.load或创建一个新变量并调用它,但是任何尝试都无法成功。

I'll be pleasure for any idea. 任何想法我都会很高兴。


UPDATE 更新

  • Please check those screen-shot: UI and code structure 请检查以下屏幕截图: UI代码结构
  • I'm trying to get this full URL generated by getSelectCountry function (1) and filter cities query on citycombo with this new returned URL (2) . 我试图获取由getSelectCountry函数(1)生成的完整URL ,并使用此新返回的URL (2)过滤citycombo上的城市查询。

If it's all about that tiny parameter, what you really want to do is populate the extraParams property in the beforeload listener of the store. 如果全部与该微小参数有关,那么您真正想要做的就是在商店的preload侦听器中填充extraParams属性。

listeners: {
    beforeload: function(store) {
        // Get countryRecord from somewhere...
        store.getProxy().setExtraParams("countryid", countryRecord.get('id'))
    }

If the server/pathname is also different, you can also setUrl() . 如果服务器/路径名也不同,则也可以setUrl()

Relevant fiddle: https://fiddle.sencha.com/#view/editor&fiddle/2c8a 相关小提琴: https : //fiddle.sencha.com/#view/editor&fiddle/2c8a

UPDATE: 更新:

So, what you really want to do: 因此,您真正想做的是:

    xtype: 'combobox',
    queryMode: 'local',
    fieldLabel: 'Country',
    store: ...,
    listeners: {
        select: function(combo, countryRecord) {
            var cityStore = combo.nextSibling().getStore();
            cityStore.getProxy().setExtraParam('countryId', countryRecord.get('Id'));
            cityStore.load();
        }
    },
},{
    xtype: 'combobox',
    queryMode: 'local',
    fieldLabel: 'City',
    store: ...,

I see two possibilities: 我看到两种可能性:

  1. Extend Alexander's solution. 扩展亚历山大的解决方案。 Assuming the cityCombo has the beforeload callback defined setting the url or extraparams , You define a change listener on the country combobox, and calling cityCombo's load() from there. 假设cityCombo在设置urlextraparams的前提下定义了beforeload回调,则可以在country组合框上定义一个change侦听器,然后从此处调用cityCombo's load()

See below: 见下文:

listeners: {
    change: function () {
        Ext.getStore('cityCombo')
            .load()
    }
}    

Or even better, remove the beforeload from the store and put what's there in the change of the first combobox 甚至更好的是,从商店中删除beforeload ,并将其中的内容放入第一个comboboxchange

listeners: {
    change: function (combo, newValue) {
        var cityStore = Ext.getStore('cityCombo')
        cityStore.getProxy()
            .setUrl(getSelectedCountry());

        cityStore.load();
    }
}

Note: you could refactor it even further, getting rid of the getSelectedCountry() and doing all of it on the combo passed by the listener 注意:您可以进一步重构它,摆脱getSelectedCountry()并在侦听器传递的组合上完成所有操作

  1. Use filter on the cityCombo instead of the round trip cityCombo上使用过滤器,而不是往返

Edit: Fixed display of the first code block, addd missing load() call. 编辑:固定显示第一个代码块,增加了缺少的load()调用。

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

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