Global Arrays
Thanks for the great answers here…
I have a similar project, except that I'm trying to pass an array of objects, so:
I would like something like:
Ti.App.friends[8342873] = {
name: "Mike",
age: 26,
height: 6.1
}
But when I call:
Ti.App.friends[dynamic.id].name
I get an error friends [undefined]. Any ideas?
I tried another syntax:
Ti.App.friends = [{
name: "Mike",
age: 26,
height: 6.1
}];
But then I have to call:
friends[0].name
Which works, except I need to call each friend by id, not by the index of the array.
I would really appreciate some help on this, thanks!
5 Answers
-
try something like this
var dict = {}; dict['a'] = {name:"Aaron", number:100}; dict['b'] = {name:"Andrea", number:101}; alert(dict['b']);
-
try
Titanium.App.Properties.setList('dict', dict);
and retrieve using
Titanium.App.Properties.getList('dict');
-
might be a better way, but I tested this and it does work with properties
var dict = {}; dict['a'] = {name:"Aaron", number:100}; dict['b'] = {name:"Andrea", number:101}; alert("first time " + dict['b'].name); // convert to string and save as property Ti.App.Properties.setString('data',JSON.stringify(dict)); // convert back into object and do your thing var newDict = JSON.parse(Ti.App.Properties.getString('data')); alert("second time " + newDict['b'].name);
-
Michael
It's difficult to understand why you were attempting to store data in Ti.App.friends, when Titanium.App.Properties of the same Ti.App object was designed specifically for that purpose.
Otherwise, if you have a lot of data to store and retrieve, why not use Titanium.Database? I can only imagine that you are concerned about performance, but databases were designed to be queried; it's what they are good at. Why not try it and at least use it as a benchmark to improve upon?
Alternatively, you can pass data from the global context using the technique described in the Titanium.UI.Window documentation.
If none of these are appliable, you will need to give us a better insight into what you are trying to achieve.
-
I would include underscore.js
Then you can do this:var myFriend = _.detect(Ti.App.friends, function(friend){ return friend.id == dynamicId }); alert(myFriend.name);