1. #1
    Sencha User
    Join Date
    Oct 2010
    Posts
    80
    Vote Rating
    0
    linuxyf is on a distinguished road

      0  

    Default how to call same a function from several button tab event?

    how to call same a function from several button tab event?


    there are several buttons in a view, i want to do same thing when i tab these buttons, so i want to invoke a function to finish this task.
    xtype:'button',
    name: 'delbtn',
    text:'DEL',
    handler: function() {
    limit = 'ok' + this.getText();
    Test(limit);
    }

    limit: ''
    Test: function(mi){
    Ext.Msg.alert('me', mi);
    }

    i get the exception: Uncaught ReferenceError: Test is not defined

    how to call same function from several button click event?

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,330
    Vote Rating
    847
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    MVC would be perfect for this!
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha Premium Member
    Join Date
    Nov 2008
    Posts
    58
    Vote Rating
    0
    mishoboss is on a distinguished road

      0  

    Default


    Why should be used MVC to just to do that?

    I have a button and I have attached a handler to it: handler: buttonPressed

    var buttonPressed = function(){
    }

    Now the question is how to send some parameters to this function? Or how the function knows which button initiated it?

  4. #4
    Sencha - Sencha Touch Dev Team rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,158
    Vote Rating
    6
    rdougan will become famous soon enough

      0  

    Default


    MVC is perfect because you can use ComponentQuery to add a listener to many components, using a simple selector.

    As for your problem; your first post just will not work. You are defining your function inside the configuration block, which just isn't how javascript works. You need to define it outside that block, or extend the class and define it in there.

    Code:
    var foo = function(a, b) {
        console.log(a, b);
    };
    
    Ext.Viewport.add({
        xtype: 'container',
        items: [
            {
                xtype: 'button',
                handler: foo
            }
        ]
    });
    Code:
    Ext.define('MyButton', {
        extend: 'Ext.Button',
    
        config: {
            text: 'Tap me!'
        },
    
        initialize: function() {
            this.callParent();
    
            this.on({
                scope: this,
                tap: function() {
                    this.foo('a', 'b');
                }
            });
        },
    
        foo: function(a, b) {
            console.log(a, b);
        }
    });
    
    Ext.Viewport.add(Ext.create('MyButton'));
    Sencha Inc.
    Robert Dougan - @rdougan
    Sencha Touch 2 and Ext JS 4 Core Team Member, SASS/Theming Wizard.

  5. #5
    Sencha Premium Member
    Join Date
    Nov 2008
    Posts
    58
    Vote Rating
    0
    mishoboss is on a distinguished road

      0  

    Default


    I just can't get it...
    For example I tried your first code. I made a handler that points to a function created outside the block. The function fires (the function fired also inside the block), but how can I pass some parameters to it? Where I send these 'a' and 'b'?

  6. #6
    Sencha - Sencha Touch Dev Team rdougan's Avatar
    Join Date
    Oct 2008
    Posts
    1,158
    Vote Rating
    6
    rdougan will become famous soon enough

      0  

    Default


    Here is a very simple example:

    Code:
    Ext.setup({
        onReady: function() {
            var foo = function(a) {
                console.log(a);
            };
    
            Ext.Viewport.add({
                xtype:'button',
                text: 'Hello',
                handler: function() {
                    foo(this.getText());
                }
            });
        }
    });
    Sencha Inc.
    Robert Dougan - @rdougan
    Sencha Touch 2 and Ext JS 4 Core Team Member, SASS/Theming Wizard.