[ALOY-1724] Add global event handlers
GitHub Issue | n/a |
---|---|
Type | New Feature |
Priority | n/a |
Status | Open |
Resolution | Unresolved |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | n/a |
Labels | n/a |
Reporter | Naman Vishoi |
Assignee | Abir Mukherjee |
Created | 2020-02-20T16:53:29.000+0000 |
Updated | 2020-03-02T15:25:24.000+0000 |
Description
It'd be great if there is an option to add global event handlers. For example - if on 'close' event of all the windows, I want to execute a line of code, I should be able to do something like this:
Ti.UI.windowClose = function(e) {
//Do something
}
Hi, I think I created the issue in wrong board. Can you move this to Titanium SDK/CLI board ? I don't see that option in the drop down while creating the issue?
[~nvishnoi] thanks for raising the ticket. Only valid tickets go to the TIMOB project in Jira. This is the inbox so it is the correct location. You can already do global events using
Ti.App.addEvenListener
andTi.App.fireEvent
. You could trigger the event from every window and monitor it at a central place. Something likeTi.App.fireEvent("closewindow", payload)
An even better method of globals events would be using Backbone events, so you don't need to "cross the bridge" for every event. See this blog post: https://devblog.axway.com/mobile-apps/global-app-events-in-titanium-using-backbone-js/ If this doesn't answer your question, feel free to reply. Otherwise, I think we can close this ticket! In case you're not in the community yet, join it at https://tislack.org. This is a perfect place for a question like thisI think [~nvishnoi] is talking about adding an event once and have it run each time a window was opened or closed, without having to add it each time you created a window. I agree that Ti.App.addEventListener and Ti.App.fireEvent should not be used (due to bridge crossing). I am not aware of this being possible with using Backbone events, but maybe I am missing something. I think this would be a great feature to add to Alloy as it would only require a few lines of code and would allow even more extensibility.
Oh right, in that case, you could override the
Alloy.createController
method, watch for Window closes dynamically and fire a backbone event. Currently, Alloy doesn't have a built-in global event handler, but that could be PR'd in indeed.