[TIMOB-18039] Add `once`, `on` and `off` to Titanium proxies
GitHub Issue | n/a |
---|---|
Type | New Feature |
Priority | Low |
Status | Open |
Resolution | Unresolved |
Affected Version/s | Release 3.4.1 |
Fix Version/s | n/a |
Components | iOS |
Labels | TCSupport, events, listeners |
Reporter | Fokke Zandbergen |
Assignee | Unknown |
Created | 2014-11-12T10:36:08.000+0000 |
Updated | 2018-02-28T19:56:00.000+0000 |
Description
When digging through some source code I discovered Android has a hidden
once()
method to add an event listener that only fires once and removeAllListeners()
to remove all listeners to a certain event:
- http://www.tidev.io/2014/11/10/hidden-event-methods-on-android/
Why aren't these methods documented and why not bring these to all platforms?
Both have very good use cases to me in almost every app.
I'd even suggest the following:
view.once('click', doClick); // fires once
view.on('click', doClick); // alias for addEventListener()
view.off('click', doClick); // alias for removeEventListener()
view.off('click'); // removes all listeners for the click-event
view.off(); // removes all listeners for any event
The last one would make cleaning up and preventing memory leaks a lot easier as well.
Hi Fokke. I my very personal opinion, I don't like it very much the aliases policies. Why you want alias? Why we need to have two or three methods to call the same thing? Noobs, like me one year ago, are lost when they see different code from different people calling different methods to do the same. You ask yourself what this "new" methods does different from the original and you need to go to docs to see that... does the same. I prefer to have a unique method to do a unique thing (*addEventListener* yes, *addListener*, *on* not). Different case is the *once* method, that does different things. Except for the aliases, will be good to have the rest of functions.
I agree we shouldn't have too many aliases. However, the name
once
comes from BackBone which is also used for Alloy controllers, where people already useon
and {off}}. That plusremoveEventListener
no longer does what the name says if you can also remove multiple listeners from one or all events.Proof of Concept: https://github.com/appcelerator/titanium_mobile/compare/master...hansemannn:TIMOB-24038 As discussed in TIMOB-24038, they could both coexist like they do in Node JS.