2

나는 하는 index.js에 다음 코드를했다 : 나는 foo.js을에티타늄/합금 : 창에 이벤트 리스너를 추가

var win = Alloy.createController('foo').getView(); 
win.open(); 
win.addEventListener('exampleEvent', function() { 
    Ti.API.info('Event Run!'); // does not seem to run 
}); 

다음과 같습니다

function runEvent() { 
    $.trigger('exampleEvent'); 
    $.getView().close(); 
} 

// execute runEvent() somewhere later 

그러나 이벤트 수신기의 함수가 실행되지 않는 것처럼 보입니다.

내가 뭘 잘못하고 있니?

답변

3

사용자 지정 이벤트는보기가 아닌 컨트롤러에만 추가 할 수 있습니다. 이 라인에서

var win = Alloy.createController('foo').getView(); 

, 당신은 변수를 승리 의 getView() 에서를 사용하여보기를 들고 있습니다. 이제

,이 같아야합니다

var win = Alloy.createController('foo'); 

win.on('exampleEvent', function() { 
    Ti.API.info('Event Run!'); // it will run now as you have added custom event on controller (means $ in .js file) itself. 
}); 

// now you can get the top-most view (which is a window in this case) and can further use open() method on window 
win.getView().open(); 

foo.js가 동일하게 유지됩니다 내 경우

function runEvent() { 
    $.trigger('exampleEvent'); 
    $.getView().close(); 
} 

// execute runEvent() somewhere later 
0

, 내가 사용하고 있었다

var controller = Alloy.createController('myController'); 
controller.addEventListener("customEvent",function(){}); 
그것이 있어야하므로

내가 지난 시간 동안 내 머리를 때린 봤는데 ... @PrashantSaini 제시 무엇의 위에

, 컨트롤러 객체에 아무런하여 addEventListener가이야없고, 컨트롤러는 on 기능을 가지고 :

controller.on("customEvent",function(){}); 

갱신

내 대답은 사실의 머리를 거기 해당 컨트롤러 객체에 아무런하여 addEventListener.

+0

@ 어떻게 그게 다르냐는 것을 알 수 없습니다. @ Sashinha Answer –

+0

업데이트 확인 – TheFuquan