2016-11-09 2 views
0

У меня есть следующая функции декларацииПередача аргумента заявленной функции

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

ответ

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 для использования.

Смежные вопросы