Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-17454] iOS: Cannot use "this" for assignments in subcontext

GitHub Issuen/a
TypeBug
PriorityNone
StatusOpen
ResolutionUnresolved
Affected Version/sRelease 3.3.0
Fix Version/sn/a
ComponentsiOS
Labelsn/a
ReporterTony Lukasavage
AssigneeUnknown
Created2014-08-04T19:48:28.000+0000
Updated2018-02-28T20:03:59.000+0000

Description

problem

*DISCLAIMER:* This description is gonna sound confusing until you read the (simple) error and working cases. When a subcontext is created using the url property of a call to Ti.UI.createWindow(), you cannot assign this from within the subcontext to Ti.UI.currentWindow or it will result in a Kroll error. You can however assign a function that returns this in the subcontext, and then that function can be invoked in the original context to get the subcontext's this. I have not tested this on any other platforms.

error case

app.js

var win = Ti.UI.createWindow({ url: 'context.js' }).open();

context.js

Ti.UI.currentWindow.__context = this;

result

[DEBUG] 2014-08-04 15:34:40.449 conty[50898:4e0b] -[KrollContext target]: unrecognized selector sent to instance 0xd336f10
[ERROR] Script Error {
[ERROR]     backtrace = "#0 () at :0";
[ERROR]     line = 6;
[ERROR]     message = "-[KrollContext target]: unrecognized selector sent to instance 0xd336f10";
[ERROR]     sourceId = 349233056;
[ERROR]     sourceURL = "file:///Users/tlukasavage/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/5EB2D06A-CE21-4B5C-A0B1-CF9BB995DF8C/conty.app/context.js";
[ERROR] } 

working case

The working case here is a little longer so as to show not only does it not cause an error in context.js, but also that the context returned from it is able to be used and iterated over when returned to app.js.

app.js

var win = Ti.UI.createWindow({ url: 'context.js' });
win.addEventListener('open', function() {
	Ti.API.info(Object.keys(win.__context()));
});
win.open();

context.js

var self = this;
Ti.UI.currentWindow.__context = function() {
	return self;
};

result

[INFO]  (
[INFO]      self,
[INFO]      Kroll,
[INFO]      setTimeout,
[INFO]      setInterval,
[INFO]      clearTimeout,
[INFO]      clearInterval,
[INFO]      require,
[INFO]      L,
[INFO]      alert,
[INFO]      Titanium,
[INFO]      Ti,
[INFO]      console
[INFO]  )

Additional Notes

This is likely related to TIMOB-17449.

Comments

No comments

JSON Source