Description
On Windows, when trying to inspect an event that is returned to an event listener a user will be faced with one of the two following results.
var win = Ti.UI.createWindow();
var label = Ti.UI.createLabel({
backgroundColor:'darkgray',
text: 'Your Test here',
});
win.add(label);
label.addEventListener('click', function(e) {
console.log(e.source);
console.log(JSON.stringify(e.source));
});
win.open();
[INFO] [object class TitaniumWindows::UI::Label]
[ERROR] : Application Error: "Runtime Error: JSON.stringify cannot serialize cyclic structures."
However, when trying to log an event on iOS and Android the following is returned
[INFO] : {"wordWrap":true,"textAlign":"center","text":"Your Test here","ellipsize":2,"enabled":true,"visible":true,"touchEnabled":true,"backgroundColor":"darkgray","backgroundRepeat":false,"size":{"height":100,"width":360,"y":0,"x":0},"rect":{"height":100,"width":360,"y":229,"x":0},"keepScreenOn":false,"children":[],"width":"fill","height":100,"bubbleParent":true,"apiName":"Ti.UI.Label","_events":{"click":{}}}
[INFO] : {"wordWrap":true,"textAlign":"center","text":"Your Test here","ellipsize":2,"enabled":true,"visible":true,"touchEnabled":true,"backgroundColor":"darkgray","backgroundRepeat":false,"size":{"height":100,"width":360,"y":0,"x":0},"rect":{"height":100,"width":360,"y":229,"x":0},"keepScreenOn":false,"children":[],"width":"fill","height":100,"bubbleParent":true,"apiName":"Ti.UI.Label","_events":{"click":{}}}
[INFO] [object TiUILabel]
{"horizontalWrap":true,"visible":true,"height":100,"backgroundColor":"darkgray","width":"FILL","textAlign":"center","text":"Your Test here","touchEnabled":true}
Steps to reproduce
Add the code above to an exisiting app.js
Build using appc run -p windows -T wp-emulator
Actual result
When attempting to get information on the event returned from the event listener, either
When attempting to use JSON.stringify an application error will be thrown as the JSON contains cyclic structures
When logging just the event, the object type will be logged
Expected result
In parity with other platforms and to aid a developer I would expect to be able to log an event to the console to inspect the properties returned in the JSON payload.
For the cyclic structure, I can see that
e.source.parent
property could cause the issue becausee.source.parent.children
obviously containse.source
itself. I would rather be surprised that iOS and Android implementation doesn't dump entire properties but only few properties onJSON.stringify
. It seems we have been using custom implementation forJSON.stringify
for them, and we might want to emulate what iOS does on Windows to fix this issue. FYI When I JSON.stringifyTi.UI.Button
instance on Windows, I got a lot more properties compared to the other platforms:https://github.com/appcelerator/titanium_mobile_windows/pull/891
So the actual issue here is that we were not able to
JSON.stringify
Ti.UI.Views. I noticed that Ti.UI.View and its subclasses hasparent
andchildren
property, and they are enumerable. That makes it impossible to log them throughJSON.stringify
because of circular references. We can easily workaround it by configuring them non-enumerable.Verified improvement with the code provided in the description. *Windows: console.log(JSON.stringify(e))*; now displays the following information:
*Environment*
Closing ticket.