Reputation: 1461
I am trying to use Appcelerator Titanium to build a mobile app. This app will be large and to make it manageable, I want to use JavaScript classes. Currently, I have a JavaScript class that is defined as follows:
function Item()
{
this.ID = 0;
this.initialize = function(id) {
this.ID = 1;
}
this.Submit = function(submitHandle) {
submitHandle();
};
}
I then invoke this class using the following:
alert("building Item");
var i = new Item();
alert("initializing Item");
i.initialize(1);
alert("submitting");
i.Submit(itemSubmitted);
function itemSubmitted() {
alert("tada!");
}
The alert message that says "buidling item" appears. However, "initializing item" never shows. In addition, my item is never submitted. I do not get an error. What am I doing wrong?
Upvotes: 0
Views: 2167
Reputation: 1130
I've put this code in a blank app.js file using Titanium Mobile SDK 1.7.5 and it works as expected in the iPhone Simulator. On the Android Emulator using Android 2.1 you only get the last alert. If I change the code to use debug statements, I can see them all firing:
Ti.API.info( "building Item");
var i = new Item();
Ti.API.info("initializing Item");
i.initialize(1);
Ti.API.info("submitting");
i.Submit(itemSubmitted);
Log output:
11-08 08:52:48.520: INFO/TiAPI(1319): (kroll$5: app://app.js) [295,1141] building Item
11-08 08:52:48.530: INFO/TiAPI(1319): (kroll$5: app://app.js) [3,1144] initializing Item
11-08 08:52:48.530: INFO/TiAPI(1319): (kroll$5: app://app.js) [2,1146] submitting
In Titanium, alert
functions don't pause the execution context like JavaScript in the browser. So the second 'alert' will be called while the first alert is open. It could be in your code that you are trying to open 2 alerts at once which cannot be done in Titanium Mobile on Android.
Another thing I would suggest is setting your functions to variables so they are easier to pass around:
var itemSubmitted = function(){
alert("tada!");
}
Check out the excellent Forging Titanium series and Kevin Whinnery's JavaScript talk from CodeStrong for more information.
Upvotes: 1
Reputation: 6786
Try this: Instead of using this.ID = 0 in your Item class, try using just ID = 0;
Upvotes: 0