2012-12-31 2 views
0

나는이처럼 보이는의 ExtJS 클래스가 있습니다. 나는이 방법이 제대로라고통화 기능은 동적

STR에 문자열로 지정된 RuleExecutor 의 메소드를 호출해야하지만 인수가 전달되지 않습니다. 이처럼

:

//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); 
} 
+0

그것은 싱글 톤,이다 RuleExecutor.displayMessage 있도록 ('blabla') 작동합니다 : 당신은이 같은 RunRule 전화를? 또는 나는 무엇인가 놓치고있다? – asgoth

+0

어디에서 RunRule()을 호출합니까? ExtJs에 뭔가 다른 것이 적용되는지는 모르지만, 관습에 따라 함수 이름은 소문자로 시작합니다. – 11684

+1

이제'메서드가 올바르게 호출되었습니다. '라는 메시지가 표시됩니다. 죄송합니다! – 11684

답변

1

이 당신이 찾고있는 무엇인가?

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

변수 이름으로 'arguments'를 사용하지 마십시오. 이미 JavaScript에는 'arguments'이라는 배열과 같은 객체가 내장되어 있습니다. 귀하의 방법은 다음과 같을 수 있습니다

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

은 내가 '진짜'배열 프로토 타입에서 slice 방법을 사용했다. 선 다음 args 변수에 첫 번째를 제외하고

args = slice.call(arguments, 1) 

복사 모든 인수.

RunRule("displayMessage", "Hello");