passing around window context in mobile
Hi,
I'm trying to get to grips with windows and contexts in Titanium Mobile.
For some reason I can't use Ti.UI.currentWindow in certain places.
eg:
in app.js:
I create an event listener that is suppose to show an activity indicator in the rightNavButton
Titanium.App.addEventListener('show_indicator', function(data)
{
Ti.API.info("show_indicator fired");
var actInd = Titanium.UI.createActivityIndicator({
height:10,
width:34,
style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
});
actInd.show();
Ti.UI.currentWindow.rightNavButton = actInd;
});
when I try to call this from a seperate window, 'win1' in win1.js
Titanium.App.fireEvent('show_indicator');
I get a big red error screen.
Anyone able to point out what i'm doing wrong please?
cheers
5 Answers
-
Sorry, i mistook the error message (i tried a workaround by passing in the currentWindow via the event params but a null error occured). Instead, there is no error. What happens is that processing seems to stop on
Ti.UI.currentWindow.rightNavButton = actInd;
I put the following line directly after that call:
Ti.API.info("show_indicator end");
and the message never appears.
-
Hi
When i call FireEvent i normally pass the data i want into the event, as the handler is expecting data.
var yourData = {};
yourData.FirstName = 'Joe';
yourData.Surname = 'Soap';Titanium.App.fireEvent('show_indicator', yourData)
I Have noticed that processing stops if in the yourData object contains a function reference
eg yourData.MyWin = function() { return Titanium.UI.currentWindow; }
Im not sure if the yourData object ie the event data simply doesnt allow the passing of references.
I have noticed that the titanium developer tries to print the results of the event data to the
screen in the developer and have a suspicion that it might be an error printing a function reference to screen
which obviously cant be done, then again the function may pass everything only by value meaning that an
actual copy of the yourData variable is made and any changes to it dont effect the one passed in. eg if you changes
firstname to 'Mark' in your handler it wouldnt be exposed to the firing procedure as its by value.Taking any references out of the yourData variable should work. If you dont have any data try passing null in
Titanium.App.fireEvent('show_indicator', null)Let me know how you go.
-
I did try passing the currentWindow in as an event parameter however i get the following error:
[ERROR] While executing Timer, received script error. '* -[NSCFArray initWithObjects:count:]: attempt to insert nil object at objects[0] at win1.js (line 3)'The event fire:
var myData = {myWin:Titanium.UI.currentWindow }; Titanium.App.fireEvent('show_indicator', myData);
and the event:
Titanium.App.addEventListener('show_indicator', function(data) { Ti.API.info("show_indicator fired"); var actInd = Titanium.UI.createActivityIndicator({ height:10, width:34, style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN }); actInd.show(); data.myWin.rightNavButton = actInd; Ti.API.info("show_indicator end"); });
However, if I assign the activity indicator to the window in the local context, everything works fine.
-
What error are you getting?
-
Hi
When i call FireEvent i normally pass the data i want into the event, as the handler is expecting data.
var yourData = {};
yourData.FirstName = 'Joe';
yourData.Surname = 'Soap';Titanium.App.fireEvent('show_indicator', yourData)
I Have noticed that processing stops if in the yourData object contains a function reference
eg yourData.MyWin = function() { return Titanium.UI.currentWindow; }
Im not sure if the yourData object ie the event data simply doesnt allow the passing of references.
I have noticed that the titanium developer tries to print the results of the event data to the
screen in the developer and have a suspicion that it might be an error printing a function reference to screen
which obviously cant be done, then again the function may pass everything only by value meaning that an
actual copy of the yourData variable is made and any changes to it dont effect the one passed in. eg if you changes
firstname to 'Mark' in your handler it wouldnt be exposed to the firing procedure as its by value.Taking any references out of the yourData variable should work. If you dont have any data try passing null in
Titanium.App.fireEvent('show_indicator', null)Let me know how you go.