将参数传递给声明的函数

时间:2016-11-09 15:10:03

标签: javascript extjs6

我有以下功能声明

var exportStore = function (exportVar) {

    // Process export .cfc

    var params_JSON =  {
      <cfoutput>
      l_companyid: '#url.companyid#',
      l_start: '#l_start#',
      l_end: '#l_end#'
      </cfoutput>
    };

    if(exportVar == 'results') {
        var exportQuery = Ext.getCmp('query');
        var query = exportQuery.getValue();
        params_JSON.query = query;
    }

    <cfoutput>var url = 'some url parameters' + Ext.JSON.encode(params_JSON)';</cfoutput>

    //ajax call here returns link to export file

    // display export link
    var myForm = new Ext.form.Panel({
        title: 'Export File',
        width: 300,
        height: 200,
        floating: true,
        closable : true,
        layout: {
            type: 'hbox',
            pack: 'center'
        },
        items: [{
            xtype: 'displayfield',
            name: 'export_file',
            value: 'Click <a href="'+export file+'">here</a> to download file'
        }],
        buttons: [{
            margin: '0 10 10 0',
            text: 'Close',
            handler: function() {

                this.up('form').destroy();

            }
        }]
    });

我正在尝试从具有下拉选项的按钮调用此函数。

 {
    text: 'Export All',
    handler: exportStore
 },
 { 
    text: 'Export Search Results',
    handler: exportStore
 } 

我的问题是你可以将参数传递给声明为变量的函数吗?我知道我可以给两个按钮自己的处理程序,但该处理程序将包含相当多的代码并且我试图简化...只是想知道参数是否可以以某种形式传递给exportStore,例如。 ....

{
        text: 'Export All',
        handler: [ 
        exportStore,
        extraParams: { exportVar: 'all' }
        ]
}

1 个答案:

答案 0 :(得分:0)

您可以使用Ext.bind()来实现此目标,如下所示:

var exportStore = function (exportVar) {
    // exportVar will have your 'all' or 'search' value as per the 
    // button clicked
    ...
}

// In definition
{
    text: 'Export All',
    handler: Ext.bind(exportStore, undefined, ['all'])
}, {
    text: 'Export Search Results',
    handler: Ext.bind(exportStore, undefined, ['search'])
}

您可以参考this fiddle了解用法。

相关问题