Search code examples
arraysglobal-variablestitaniumappcelerator-mobile

how to update global variable in titanium?


i'm having some problem in updating my array which is global by the way.

here is my code:

Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];

thats my global array which i can access data from it from anywhere in the application.

the problem comes when i want to update the array like:

for(var q=0; q<Ti.App.dinercolor.length; q++){Ti.App.dinercolor[q] = '#dccdc0';}    

so, the array i was expecting after the operation thats done is something like this:

Ti.App.dinercolor=["#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0"];

but somehow i'm getting the same array with out updating,

Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];

please help me out, i have no idea what i'm doing wrong here,

Thank you,,


Solution

  • Your code is correct, but you shouldn't extend the Ti object as unexpected things like this will happen. Create your own object and it will work.

    myObj = {};
    myObj.dinercolor = [];
    

    And so on.

    It is recommended you keep your app in a single context so you will be able to access the object from anywhere. Check out the forging titanium video series for some best practices.