Results 1 to 2 of 2

Thread: Passing a variable to a function declared as a variable

  1. #1
    Sencha User
    Join Date
    Dec 2015
    Location
    Norther Virginia
    Posts
    15

    Default Answered: Passing a variable to a function declared as a variable

    Good Morning,


    I have the following function declaration


    Code:
     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();
        
                    }
                }]
            });


    I am attempting call this function from a button that has a drop down selection.


    Code:
    {
            text: 'Export All',
            handler: exportStore
         },
         { 
            text: 'Export Search Results',
            handler: exportStore
         }
    My question is can you pass a parameter to a function declared as a variable? I know I can just give both buttons their own handler, but that handler is going to contain quite a bit of code and im attempting to simplify... just wanted to know if a parameter can be passed to exportStore in some form, for example.....



    Code:
      {
                text: 'Export All',
                handler: [ 
                exportStore,
                extraParams: { exportVar: 'all' }
                ]
        }
    Thanks in advance......

  2. A couple of options:

    1) Wrap it in a anonymous function:
    Code:
    handler: function() {
        exportStore('foo');
    }
    2) Use bind:
    Code:
    handler: exportStore.bind(null, 'foo')

  3. #2
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,250
    Answers
    758

    Default

    A couple of options:

    1) Wrap it in a anonymous function:
    Code:
    handler: function() {
        exportStore('foo');
    }
    2) Use bind:
    Code:
    handler: exportStore.bind(null, 'foo')
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

Similar Threads

  1. Replies: 1
    Last Post: 17 Aug 2014, 3:56 AM
  2. confirm() callback function not passing button variable
    By noromamai in forum Sencha Touch 1.x: Q&A
    Replies: 4
    Last Post: 12 Dec 2011, 2:04 PM
  3. Passing a variable by MVC
    By dosuper in forum Ext: Discussion
    Replies: 3
    Last Post: 14 Sep 2011, 6:47 AM
  4. HELP!!! passing a variable from a function to the main?!?!
    By grubby69 in forum Sencha Touch 1.x: Discussion
    Replies: 1
    Last Post: 13 Sep 2011, 3:25 AM
  5. problem with passing a variable
    By koolaid1551 in forum Sencha Touch 1.x: Discussion
    Replies: 2
    Last Post: 29 Jul 2011, 10:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •