Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-13338] Ti API: implement features to add several listeners to a proxy in one call

GitHub Issuen/a
TypeNew Feature
PriorityLow
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsTiAPI
LabelsAPI, Titanium, listeners
ReporterVincent
AssigneeUnknown
Created2012-10-26T09:44:19.000+0000
Updated2018-02-28T20:03:30.000+0000

Description

for example, it would be nice to have a method like Titanium.Proxy.addEventListeners( { } );
var self = Ti.UI.createWindow({
		
	}); 


self.addEventListeners(
	{
	 blur : function(e) { Ti.API.warn( JSON.stringify (e) ) } ,
	
	focus: function(e) { Ti.API.warn( JSON.stringify (e)) },

	myEvent : function(e) { Ti.API.warn(  JSON.stringify (e)) },
	}


);
to be clear I know, this can be done in Javascript ( but this means adding a method to Titanium.Proxy ) , and the idea here is not to call addEventListener in a loop but rather cross the bridge only once receiving a NSDictionary to transform callbacks in KrollCallbacks ( something like applyProperties()). for IOS my first try was to do :
-(void)addEventListeners:(id)args
{
    
    ENSURE_SINGLE_ARG(args, NSDictionary);
    
    for (NSString * currentKey in args)
    {
        KrollCallback*  currentListener = [args objectForKey:currentKey];
        ENSURE_TYPE(currentListener,KrollCallback);
        
        [self addEventListener: [NSArray arrayWithObjects:currentKey, currentListener, nil] ];
        
    }
    
}
This seems to work, but i'm safe to do that ? -Moreover would it be possible to pass a Dictionary of eventListeners in the Ti factory methods ?
Ti.UI.createXXX({
propertyX : value
...
eventListeners : { .... } // is this possible for UI events ? custom events ? 

})
In some case , this could ensure that the listeners are registered before the events occur ( Ti.UI.Window events for example ) - I saw that Alloy implement the on(..) off(..) method with event listeners, so another solution could be to use the same syntax in the Proxy create method:
Ti.UI.createXXX({
propertyX : value
...
onClick = function(){}
...

})

Comments

  1. Ivan Skugor 2013-04-04

    Maybe array of event listeners would be better since there can be multiple event listeners per event type. For example:
       self.addEventListeners([
           { click : function(e) { Ti.API.warn( JSON.stringify (e) ) } },
            
           { click: function(e) { Ti.API.warn( JSON.stringify (e)) } }, 
        
           { myEvent : function(e) { Ti.API.warn(  JSON.stringify (e)) } }
       ]);
       
  2. Bryan Hughes 2013-04-04

    We have been considering making the templating system that ListView uses available for all controls, and this dovetails nicely since templates already support listing multiple event listeners. Even if we don't make templates general purpose, this call should probably conform to the ListView template spec for uniformity's sake.
  3. Ingo Muschenetz 2014-03-04

    We will revisit this at a later point when there is significant customer requests.

JSON Source