Search code examples
javascripttitaniumappcelerator

Appcelerator issue: currentWindow : Titanium.UI.Window - REMOVED from Titanium since SDK 6.0.0


According to the documentation, currentWindow has been removed along with the Titanium.UI.Window.url property to load JavaScript files in their own contexts. I have successfully removed the URL references using require() instead.

I have inherited a project that refers to currentWindow for managing different page assets, like so:

var thisWindow = Titanium.UI.currentWindow;
// var thisWindow = Ti.UI.currentWindow; - also doesn't work
var nav = Titanium.UI.iOS.createNavigationWindow({
    window: thisWindow
});

thisWindow.nav = nav;
nav.open();

var detailWindow = Ti.UI.createWindow({
    backgroundColor: '#fff',
    backButtonTitle: '',
    navTintColor: '#FFF',
    barColor: '#222222',
    getURL: 'http://google.com',
    titleControl: Ti.UI.createLabel({
         //text: 'TITLE',
         color: '#FFF',
         font: {
              fontSize: 16,
              fontWeight: 'bold'
         }
     })
  });

  detailWindow = require('details');
  // add the detail to the nav window
  detailWindow.nav = thisWindow.nav;

Here are the contents of details.js:

var window = Ti.UI.currentWindow;
var website = window.getURL;

var webview = Titanium.UI.createWebView({
    backgroundColor:"#fff",
    url:website
});

window.add(webview);

And as this is no longer supported I'm getting the error:

undefined is not an object (evaluating 'thisWindow.nav=nav')

How do I bring this method up to date (ideally without a huge rewrite, as there are many pages and page-elements linked in this way).

Thanks!


Solution

  • You can use the commonjs structure.

    If you need to pass a window from one file to the other, provide it using setters.

    // main.js
    var details = require('details');
    details.setWindow(myWindowVar);
    
    // details.js
    var window;
    exports.setWindow = function(win){
        window = win;
    }
    

    The other way around using get aso works of course.

    // details.js
    exports.window = window;
    
    // main.js
    detailWindow = require('details').window;
    

    Keep in mind though, when using require it is loaded in memory. When requireing again later, you're hitting the same thing, it won't re-generate. In case you do want that you need to make functions that create it, and run that.

    //details.js
    function createWindow(){
      var win = Ti.UI.createWindow();
      return win;
    }