0

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);
}
3
  • It's a singleton, so RuleExecutor.displayMessage('blabla') should work? Or I am missing something? Commented Dec 31, 2012 at 12:41
  • Where do you call RunRule()? I don't know if something else applies for ExtJs, but by convention, function names start with a lower case letter. Commented Dec 31, 2012 at 12:45
  • 1
    Now I see The method is called correctly.... Sorry! Commented Dec 31, 2012 at 12:46

2 Answers 2

2

Don't use 'arguments' as a variable name. There already is a built-in array-like object called 'arguments' in JavaScript. Your method may look like this:

function RunRule(str) {
    var slice = Array.prototype.slice,
        args = slice.call(arguments, 1);
    RuleExecutor[str].apply(RuleExecutor, args);
}

I used the slice method from the 'real' array prototype. The line:

args = slice.call(arguments, 1)

copies all arguments except the first one to the args variable. You call RunRule like this:

RunRule("displayMessage", "Hello");
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.