I have the following code in index.js:
var win = Alloy.createController('foo').getView();
win.open();
win.addEventListener('exampleEvent', function () {
Ti.API.info('Event Run!'); // does not seem to run
});
on foo.js I have the following:
function runEvent() {
$.trigger('exampleEvent');
$.getView().close();
}
// execute runEvent() somewhere later
However, the function in the event listener does not seem to run.
What am I doing wrong?
You are missing a point that custom events can only be added on a controller, not on the view.
var win = Alloy.createController('foo').getView();
In this line, you are holding the view by using getView() in win variable.
Now, it should be like this:
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 will remain same:
function runEvent() {
$.trigger('exampleEvent');
$.getView().close();
}
// execute runEvent() somewhere later