how to update global variable in titanium? - arrays

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,,

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.

I agree with Jeff, however if you want the above approach to work you will need to update the whole array, you cannot just update elements.
So read the array out into a new variable, update the specific elements and then set the property again

In App.js:
Ti.App.my_variable = 0;
In some_other_page.js:
Ti.App.my_variable = 101;
In yet_another_page.js:
alert( Ti.App.my_variable );
This will alert 101 !!

Related

How to retrieve specific value of an array from firestore

I am retrieving specific data as an array from firestore, the value is the there on the console.log(), but i cant seems to retrieve the specific data from the array itself,
here's my event.ts
import { Event } from '../../models/event';
invitedEvents: Event[] = [];
this.invitedEvents = invitedEvents;
console.log(invitedEvents, invitedEvents.name);
on console.log()
as you can see the invitedEvents.name return value of undefined, i'm sure you guys know the proper way of retrieving the value of name, send help.
The object is in the array, so you have to access object from array first invitedEvents[0]
console.log(invitedEvents, invitedEvents[0].name);
Here you have to invitedEvents is an array. So if you need to access you can't directly like invitedEvents.name like this, here is the few ways to access.
for(let item of invitedEvents){
console.log("Data",item);
console.log("Specific Name",item.name);
}
This example is used to print multiple values.
If you have only one value.Simply do like this.
console.log("Specific Name Second way", invitedEvents[0].name);
I hope its helps you.
Thanks,
Muthu

Angular copy not working on copying array

It seems that angular.copy() is not properly working on one of the items that I am using it on. Here's the sample code and the screenshot that follows.
console.log("Copy");
$scope.traffic_data = traffic_data;
$scope.total_data = total_data;
console.log($scope.traffic_data);
console.log($scope.total_data);
console.log("Original");
$rootScope.original_traffic_data = angular.copy($scope.traffic_data);
$rootScope.original_total_data = angular.copy($scope.total_data);
console.log($rootScope.original_traffic_data);
console.log($rootScope.original_total_data);
console.log("Variable data");
console.log(total_data);
console.log("=============");
The problem I am facing is that the
$rootscope.original_total_data
is not copying the contents of the
$scope.total_data
as seen on the screenshot. I have highlighted the different console logs to differentiate them from one another.
The line
console.log($rootScope.original_total_data);
shows no contents even though I have used angular.copy on that variable.
What am I missing here? Please help. Thanks.
Also $rootScope is already declared in the controller and it is working for the
$rootScope.original_traffic_data
so why is it not working for
$rootScope.original_total_data?
Thanks.
total_data is an array, whereas traffic_data is an object.
angular.copy() distinguishes between arrays and objects. For objects it will copy all the keys (properties). For arrays, it will only copy the array elements and not any custom properties attached to it - see source code.
If you want to set properties on total_data, you should make it into an object instead. It does not appear to have any indexed values, so this should not be a problem, and it probably should have been an object in the first place.

Putting year() directly into a array

I am currently creating a array that holds a bunch of different data. One of the data points is to hold the most recent date accessed. For some reason I can't put year() into the array without the program breaking. Code below.
world_saves.push([[year()]]);
What's the reasoning behind this, and how do I set put the year into the array without a error. I've tried defining year() into a variable, but once again I get a error. Error below.
World_Data.js:5 Uncaught ReferenceError: year is not defined
Thanks in advance!
EDIT:
For anyone viewing this, this is the correct code to use.
var pjs = new Processing();
world_saves.push([[pjs.year()]]);
With the caveat that I've never used processing.js, it appears that you have to instantiate an instance of it first before calling methods on it.
Something like:
var pjs = new Processing();
world_saves.push([[pjs.year()]]);
also note: You do realize you're pushing an array of an array containing the year here, right? (vs. just world_saves.push(pjs.year());)
I see that you already got this sorted out, but just for your information: with Processing.js, typically you'd write Processing code and have Processing.js create the Processing instance and call the setup() and draw() functions for you. There are a ton of ways to do this (many are outline on this page), but the basics would look like this:
<script src="processing-1.3.6.min.js"></script>
<script type="application/processing" data-processing-target="pjs">
int[] worldSaves = new int[1];
void setup() {
size(200, 200);
worldSaves[0] = year();
}
void draw(){
background(0);
text(worldSaves[0], 10, 10);
}
</script>
<canvas id="pjs"> </canvas>
What you came up with isn't really wrong or anything, but I also don't know why you're using Processing.js if you aren't using the Processing part of it. So eventually you might end up with something more like the above.

Trouble referencing movieClips

I am working on a flash project that dynamically generates navigation from an XML. For now I am trying to get it to work with arrays so that I can adapt it to xml once I know the logic works. I am new to as3 and learning has been a tiny bit bumpy. I have been searching for a solution to this but many of the examples I have seen have either been too simple to answer my question or too complex to understand since I am on the new side. This is the code I am working with.
var clientList:Array = new Array("Client1","Client2","Client3","Client4","Client5","Client6","Client7","Client8","Client9","Client10","Client11","Client12","Client13","Client14","Client15");
for each (var cName in clientList){
var newClientBtn:btnClientNav = new btnClientNav();
newClientBtn.x = workX;
newClientBtn.y = workY;
workY += newClientBtn.height;
newClientBtn.mcClientName.text = cName;
lContent.mcWork.addChild(newClientBtn.name);
trace(newClientBtn);
}
I can't fingure out how to properly refernce the dynamically created clips. I have a dynamic text box in the button but can't figure out how to reference it properly to change it, then my next issue will be referencing the buttons to make rollover and click code. I know this probably something simple to fix but I have been looking at it for too long and my eyes are starting to cross.. Thank you in advance for any advice you can give.
Why not store the clips you are creating in an object you can access later?
If you want to reference a movie clip by name though, one way to do it is:
var referenceMC:MovieClip = MovieClip(containerMC.getChildByName(“targetMC”));
If it was a text field or a button you were after, I believe you would do the same but instead cast the result of getChildByName to your desired control.
I also believe you want to add the button itself as a child, not pass its name into your addChild call?

Accessing variable from other class returns null

I did a separate levelData class to be able to flexibly add levels. I was happy with it until my supervisor ordered me to convert my levelData into XML. I did an XML version of the levelData's data (question, answers, correct answer...). I used the old class and converted it so that it fetches the XML.
All seems well, I did traces of my answers array and it printed nicely...
But the headache started when I tried this.
// This code appears in a different class with
// currentLvl:LevelData initialized in the constructor.
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
// BTW, I can't make a for loop to do the same function as above. So wierd.
I tried to run it. it returned:
TypeError: Error #2007: Parameter text must be non-null.
at flash.text::TextField/set text()
at QuestionPane/setQuiz()
at QuestionPane/setQuestion()
at QuestionPane()
at LearningModule()
Where did I go wrong? I tried making a custom get function for it, only to get the same error. Thanks in advance. If I need to post more of the code, I will gladly do so =)
LevelData Class in PasteBin: http://pastebin.com/aTKC1sBC
Without seeing more of the code it's hard to diagnose, but did you correctly initialize the choices Array before using it? Failing that I think you'll need to post more code.
Another possible issue is the delay in loading the XML data. Make sure your data is set before QuestionPane tries to access it.
When did you call
quizHolder.ansA.ansHud.text = currentLvl.choices[1];
quizHolder.ansB.ansHud.text = currentLvl.choices[2];
quizHolder.ansC.ansHud.text = currentLvl.choices[3];
quizHolder.ansD.ansHud.text = currentLvl.choices[4];
these? You load the XML and on complete you fill the array, what is correct. but is the XML loaded and parsed to the time when you access (fill the TextFields) the choices array already?

Resources