Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Use redefined function in every opened window

Hello everybody. I am trying to make a orientationchange listener that uses a specific function for the current window, and this function should be redefined in every .js opened as a window.

The code looks something like this:

app.js


var win = Titanium.UI.createWindow({
    url:'prod_win.js'
});
Ti.Gesture.addEventListener('orientationchange',function(e) {
    Ti.API.info('orientationchange event');
    var win = Titanium.UI.currentWindow;
        win.a();
});

_prodwin.js

var win = Titanium.UI.currentWindow;
win.a = function() {
   alert('b');
}
win.a();

The call in the prod_win.js for the function win.a() happens with no problem, but the one from the trigger gives this error: "Result of expression 'win' [null] is not an object.";

Can someone tell me what I am doing wrong?
Thanks in advance.

Vali.

— asked November 3rd 2010 by Vali Filip
  • function
  • redefine
  • reuse
  • window
0 Comments

5 Answers

  • Bump

    — answered November 3rd 2010 by Vali Filip
    permalink
    0 Comments
  • Yet again. Please help me figure this out, because it would clean a lot of duplicate code that I have copied to make this work…

    — answered November 3rd 2010 by Vali Filip
    permalink
    0 Comments
  • I dont know for sure, but why are you defining "win" in your app.js twice?

    — answered November 3rd 2010 by Josh Lewis
    permalink
    0 Comments
  • I believe Titanium.UI.currentWindow will be null if it is called within app.js. Are you seeing info in the console about a rotation but get an error about win being nil or win.a is not a function?

    — answered November 3rd 2010 by John McKnight
    permalink
    1 Comment
    • The initialization of win in the trigger is for its scope, every time the trigger is called, i want win to be redefined as thr current window. The call from prod_win.js works just fine, but the one in the trigger gives the referenced error. It I do a typeof win in the trigger, it will output 'object' but it I go for typeof win.a the error reises again. Any ideas?

      Thanks in advance.

      — commented November 4th 2010 by Vali Filip
  • it looks like there is bug in Ti.Gesture.addEventListener('orientationchange',function(e) {}); scope.
    you can't get current window from inside this event function. (don't get confused if it sometimes works. its because you can get this right just for one window what is active at the time of creating this event listener. but change of current window is not reflected)
    my related question

    — answered February 21st 2011 by gondo gondo
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.