Search code examples
javascriptfunctionextjsdynamic-dispatch

call function in extjs class dynamically


I have an ExtJs class that looks like this:

Ext.define("RuleExecutor", {
    singleton: true,
    displayMessage: function(msg) {
        Ext.Msg.alert('Popup Message', msg[0]);
    },
    disableById: function(field) {
        Ext.getCmp(field).setDisabled(true);
    },
    //more functions are here...
});

Now I get a string => str which contains the method name I need to run. I need to call the method in RuleExecutor specified by the string in str

The method is called correctly, but the arguments are not passed.

Like this:

//arguments is an array
function RunRule(str, arguments) {
  //I tried this....
  var fn = RuleExecutor[str];
  fn(arguments)

  //This doesn't work either..
  RuleExecutor[str].apply(this, arguments);
}

Solution

  • Is this what you're looking for?

    Ext.onReady(function () {
        Ext.define("RuleExecutor", {
            singleton: true,
            displayMessage: function (msg) {
                Ext.Msg.alert('Popup Message', msg[0]);
            },
            disableById: function (field) {
                Ext.getCmp(field).setDisabled(true);
            }
        });
    
        var str = 'displayMessage';
        RuleExecutor[str](['bar']);
    });