Problem
When a proxy's
addEventListener
or
removeEventListener
is called via Javascript's
call()
or
apply()
functions, as demonstrated in Test Case #1, you get the following exception on iOS:
[ERROR] Script Error = 'undefined' is not a function (evaluating 'al.call(win, 'click', function(e) { alert('clicked window'); })') at app.js (line 5).
The same call will work fine on Android and Mobileweb. On the other hand, calling these function without the call() function, as demonstrated in Test Case #2, fails on Android:
[ERROR][TiJSError( 352)] (main) [607,2357] ----- Titanium Javascript Runtime Error -----
[ERROR][TiJSError( 352)] (main) [3,2360] - In ti:/events.js:176,9
[ERROR][TiJSError( 352)] (main) [7,2367] - Message: Uncaught TypeError: Object #<Object> has no method '_hasListenersForEventType'
[ERROR][TiJSError( 352)] (main) [0,2367] - Source: this._hasListenersForEventType(type, true);
[ERROR][V8Exception( 352)] Exception occurred at ti:/events.js:176: Uncaught TypeError: Object #<Object> has no method '_hasListenersForEventType'
but works on iOS and Mobileweb.
Test Case #1
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var al = win.addEventListener;
al.call(win, 'click', function(e) { alert('clicked window'); });
// al.apply(win, ['click', function(e) { alert('clicked window'); }]);
win.open();
Test Case #2
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var al = win.addEventListener;
al('click', function(e) { alert('clicked window'); });
win.open();
Expected Result
Test Case #1 for calling the
addEventListener
and
removeEventListener
functions should function properly on Android, iOS, and Mobileweb with
call()
and
apply()
.
We need to add test case for apply too
IMHO, second test case should not work since "var al = win.addEventListener;" unbinds "win" from that function (and changes "this" value to point to global object). Similar thing does not work in DOM environment. First test case opens Pandora's box. What if you do something like this:
Or this:
That should work in theory. In practice ... don't know. :)
apply()
added in Test Case #1. Ivan , you're probably right, I updated the Expected Result to indicate that Test Case #1 should work for all platforms.Complete test cases:
PR pending https://github.com/appcelerator/titanium_mobile/pull/2541