Titanium Community Questions & Answer Archive

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

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!

— asked November 18th 2010 by Michael Newell
  • arrays
  • global
  • iphone
  • objects
0 Comments

5 Answers

  • try something like this

    var dict = {};
    
    dict['a'] = {name:"Aaron", number:100};
    dict['b'] = {name:"Andrea", number:101};
    
    alert(dict['b']);
    
    — answered November 18th 2010 by Aaron Saunders
    permalink
    2 Comments
    • Thanks Aaron, I have tested this and it works, except when putting this into the global variable

      Ti.App.dict['a'] = {name:"aaron", number:100};
      

      Then all of a sudden it stops working…

      — commented November 18th 2010 by Michael Newell
    • Hi Michael, I've had no problems with simple data types (ie. Ti.App.ammo =100; etc), but i've not tried with arrays or objects. As an alternative, can you use Properties?

      — commented November 18th 2010 by karlo kilayko
  • try

    Titanium.App.Properties.setList('dict', dict);

    and retrieve using

    Titanium.App.Properties.getList('dict');

    — answered November 18th 2010 by ben nduati
    permalink
    1 Comment
    • Thanks ben!

      Unfortunately now, I'm getting a memory error:

      BunchOfReplacement(2936,0xb0353000) malloc: mmap(size=2513080320) failed (error code=12) error: can't allocate region

      any idea what that means?

      — commented November 18th 2010 by Michael Newell
  • 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);
    
    — answered November 19th 2010 by Aaron Saunders
    permalink
    2 Comments
    • You should use [] instead of {} to define an array.

      var dict = [];
      

      — commented November 19th 2010 by Jacob van Dam
    • its not an array

      — commented November 20th 2010 by Aaron Saunders
  • 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.

    — answered November 19th 2010 by Paul Dowsett
    permalink
    0 Comments
  • 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);
    
    — answered November 30th 2010 by Roger Chapman
    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.