3

I have the following ExtJS controller code:

init: function() {
    this.control({
        '#menuitem-A': { click: this.handlerA },
        '#menuitem-B': { click: this.handlerB },
    });
},

and the following event handlers:

commonFunc: function(param1, param2) {
    // do something with param1 and param2 in here
},

handlerA: function(button, event) {
    this.commonFunc('param-A1', 'param-A2');
},

handlerB: function(button, event) {
    this.commonFunc('param-B1', 'param-B2');
},

Problem: the current code is redundant: handlerA and handlerB just call commonFunc with different parameters

Question: I would like to remove handlerA and handlerB and instead call or apply the common function commonFunc with the arbitrary parameters within handlerA and handlerB functions above, for different event handlers. Is it possible?

Example:

init: function() {
    this.control({
        '#menuitem-A': { click: /*commonFunc with ['param-A1', 'param-A2']*/ },
        '#menuitem-B': { click: /*commonFunc with ['param-B1', 'param-B2']*/ },
    });
},

Many thanks!

1 Answer 1

5

how about this:

init: function() {
    this.control({
        '#menuitem-A': { 
            click: function(button, event){
                this.commonFunc('param-A1', 'param-A2');
            }
        },
        '#menuitem-B': { 
            click: function(button, event){
                this.commonFunc('param-B1', 'param-B2');
            }
        }
    });
},
Sign up to request clarification or add additional context in comments.

2 Comments

So is it impossible to set up a call to the function (when the event triggers) without actually setting up an intermediate function?
You could use Ext.Function.bind(), but it's essentially just a shortcut for doing the same thing. The answer provided above is functionally no different from what you had.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.