2011-07-31 4 views
6

Dojo를 시작하기 만하면됩니다. 두 개의 사용자 지정 매개 변수를 이벤트 처리기에 전달하고 싶습니다. jQuery를, 당신은 이런 식으로 작업을 수행 할 수 있습니다Dojo : 이벤트 처리기에 사용자 정의 매개 변수 전달 방법

$('#button').click({ 
    customData: 'foo' 
}, handlerFunction); 

그리고 customData이 같은 handlerFunction에서 액세스 할 수 있습니다

function handlerFunction(event) { 
    console.log(event.data.customData); 
} 

내가 도장에 걸쳐 jQuery 코드의 비트를 마이그레이션하고 있습니다. Dojo 이벤트 핸들러에이 매개 변수를 전달하려면 어떻게해야합니까? 도장에서 이벤트를 연결할 때 그래서

function make_event_handler(customData){ 
    return function(evt){ 
     //customData can be used here 
     //just like any other normal variable 
     console.log(customData); 
    } 
} 

:

답변

12

음, generaly, 클로저는 함수에 "숨겨진"매개 변수를 전달할 수 있도록

dojo.connect(node, 'onclick', make_event_handler(17)); 

내가 많이 좋아 또 다른 가능성 dojo.partial/dojo.hitch를 사용하여 클로저를 작성 중입니다. 이 이러한 모든 이벤트 핸들러 필요하다는

function event_handler(customData, evt){ 
    /// 
} 

dojo.connect(node, 'onclick', dojo.partial(event_handler, 17)) 

참고 염두에 추가 매개 변수 (들)을 통과 만들 수 있습니다. evt 변수를 추가로 마사지해야하기 때문에 JQuery 코드를 좀 더 직접 번역 할 수 있을지 모르겠다. dojo가 그렇게 생각하지 않는다. 또한

+1

아하,'dojo.partial' 내가 찾고 있어요 것입니다. 감사! – Jonah

1

:

this.connect(other, "onClick", function(e) { 
    /* other is accesible here still */ 
}); 

나 :

this.connect(other, "onClick", dojo.hitch(this, "handler", other); 

와 이벤트 핸들러 :

this.handler = function(other, evt){...}