Tobi
Tobi

Reputation: 168

Add eventlistener to a window in Titanium Mobile commonJS

I have a surely somehow stupid problem with adding an eventlistener to a window I create in a commonJS module in Titanium Mobile.

Consider i.e. the following code:

var SegmentListWindow = function(){
  var window = S.ui.createWindow("Testwindow");
  window.addEventListener("app:customListener", function(){ doSomething();});
  return window;
}
exports.SegmentListWindow = SegmentListWindow;

The window is nicely generated using

var Window = require(".....").SegmentListWindow;
var win = new Window();

S.ui is just a simple helper method to create some standard window in my app.

But the event listener is never called, I tryTi.App.fireEvent("app:customListener"), but the event doesn't reach the listener. Only when Using Ti.App.addEventListener and adding a global eventlistener it's working.

I think maybe that problem is I am not adding the event listener to the "instance" of the window? But how to fix this? I don't want to add the event listener manually when instantiating the window somewhere in the app. Can't I do this in the commonJS module?

Upvotes: 0

Views: 2203

Answers (2)

Miika Pihlaja
Miika Pihlaja

Reputation: 88

You could also define the SegmentListWindow as you did in the question:

var SegmentListWindow = function(){
  var window = Ti.UI.createWindow({title:"Testwindow"});
  window.addEventListener("win:customListener", function(){ doSomething();});
  return window;
}
exports.SegmentListWindow = SegmentListWindow;

and then fire the event on the win object:

var Window = require(".....").SegmentListWindow;
var win = new Window();
win.fireEvent('win:customListener');

Upvotes: 0

Tobi
Tobi

Reputation: 168

Well, that really was a simple question.

I am doing a Ti.App.fireEvent, but was listening for window.addEventListener, that couldn't work.

Now I am doing the following:

Adding an eventlistener on window instantiation to the global Ti.App-Object, and remove this listener on the window's close event. That works perfectly.

Upvotes: 1

Related Questions