Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-18039] Add `once`, `on` and `off` to Titanium proxies

GitHub Issuen/a
TypeNew Feature
PriorityLow
StatusOpen
ResolutionUnresolved
Affected Version/sRelease 3.4.1
Fix Version/sn/a
ComponentsiOS
LabelsTCSupport, events, listeners
ReporterFokke Zandbergen
AssigneeUnknown
Created2014-11-12T10:36:08.000+0000
Updated2018-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.

Comments

  1. Manuel Conde Vendrell 2014-11-13

    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.
  2. Fokke Zandbergen 2014-11-13

    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 use on and {off}}. That plus removeEventListener no longer does what the name says if you can also remove multiple listeners from one or all events.
  3. Hans Knöchel 2016-10-18

    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.

JSON Source