[TIMOB-13338] Ti API: implement features to add several listeners to a proxy in one call
GitHub Issue | n/a |
---|---|
Type | New Feature |
Priority | Low |
Status | Open |
Resolution | Unresolved |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | TiAPI |
Labels | API, Titanium, listeners |
Reporter | Vincent |
Assignee | Unknown |
Created | 2012-10-26T09:44:19.000+0000 |
Updated | 2018-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(){}
...
})
Maybe array of event listeners would be better since there can be multiple event listeners per event type. For example:
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.
We will revisit this at a later point when there is significant customer requests.