Thursday, August 01, 2013

Titanium Javascript Execution Context

Some notes from "Understanding Execution Contexts"
http://developer.appcelerator.com/blog/2010/08/execution-contexts.html

"A JavaScript application running in a web browser is single threaded and has a global variable scope for the entire application. A Titanium Mobile application is similar"

app.js creates one execution context.

" New execution contexts are created when a window is created that points to an external JavaScript file, which would then bootstrap the new window."
We call it "heavyweight" window.

var win = Titanium.UI.createWindow({
  title: 'New Window',
  url: 'win.js'
});


"When this new window is opened, it will have it’s own context and scope, so any variables declared in other contexts will not be available."

The variable defined in app.js is out of scope in win.js, because it's another execution context.


You can fire application-level events that will be received in all currently active execution contexts (app.js plus any open windows) via fireEvent. Custom events can have arbitrary data passed along with them, as shown in the example below:

Ti.App.fireEvent('myCustomEvent', {
  myCustomEventValue: 'someValue'
});

"You can listen for these custom events in any context by using addEventListener at the application level as well. The example below listens for our custom event – you’ll notice that any properties of the object passed as the second argument to fireEvent are available on the event object the callback function takes as an argument:"

Ti.App.addEventListener('myCustomEvent', function(event) {
  Ti.API.info('You sent me: '+event.myCustomEventValue);
});

"Note that only ACTIVE execution contexts will receive a custom event when it is sent, so any windows that are not yet open will not receive the event."

No comments:

Post a Comment