3

I am really confused regarding single and multiple execution context in Javascript. I referred this http://developer.appcelerator.com/blog/2010/08/execution-contexts.html though I got a good overview of JS context. But I don't know when it is good to use single or multiple context while building application in Titanium.

Ajeet Pratap Maurya
  • 4,244
  • 3
  • 28
  • 46
  • Can't comment explicitly on Titanium, but a similar situation exists in a browser, where each window and frame has its own global execution context (the linked article is incorrect when saying there is a single global context for the entire browser application, it is per window or frame). Child windows must reference global variables in the parent window as "opener.varName", as the unqualified "varName" will be resolved in the child's scope, not the parent's. – RobG Jul 26 '12 at 06:51

1 Answers1

3

From the Best Practices Documentation (emphasis mine):

With Titanium Mobile, it is possible to create a window with a url property set to a path to a Javascript file (relative to the current file). When the window's open method is called, the associated JavaScript file is evaluated, creating a secondary "execution context" and, thus, a new scope. Except in rare cases, this multiple active JavaScript environment should be avoided.

These multiple execution contexts cause problem because no scope has visibility of any other, meaning that sharing data between contexts is not possible without the ungainly use of application-level custom events (using Titanium.App addEventListener and fireEvent). They can also lead to circular references and likely memory leaks. There are lifecycle issues too, where it becomes unclear when the code for a given JavaScript file has been evaluated.

While there are a few reasonable use cases for this approach, such as an "app within an app" where every new window requires a "clean slate" with no dependencies on the global context, normally windows with URLs should not be used.

Community
  • 1
  • 1
Adam Paxton
  • 1,422
  • 1
  • 12
  • 11
  • 1
    Thanks Adam, how can you avoid multiple JS environment. If the window needs to be open it will be..any tips regarding that – Ajeet Pratap Maurya Jul 31 '12 at 06:14
  • The main thing to do is not use the url property when creating windows. Instead, go for the CommonJS modular approach like [this](https://github.com/appcelerator-developer-relations/Template.SingleWindow/blob/master/Resources/ui/handheld/ApplicationWindow.js). Android has additional things to consider regarding execution contexts, or [heavyweight windows](https://wiki.appcelerator.org/display/guides/Android+Module+Development+Guide#AndroidModuleDevelopmentGuide-HeavyweightandLightweightWindows), which was also discussed in the post you linked to in the question. – Adam Paxton Jul 31 '12 at 17:47
  • so you mean that opening a window using commonJS approach will open that window in that same context(environment). – Ajeet Pratap Maurya Aug 02 '12 at 09:32
  • Correct, provided we don't use any of the window properties mentioned previously. – Adam Paxton Aug 02 '12 at 14:54
  • "window properties used previously"..didnt get that. can you please elaborate on that. – Ajeet Pratap Maurya Aug 03 '12 at 05:03
  • The window properties _mentioned_ previously were: the `url` property and anything that creates a heavyweight window in android. – Adam Paxton Aug 06 '12 at 16:44