string to instance name in action script 3 - actionscript-3

i'm having some problems with as3
var eventChildren:XMLList = eventInput.channel.children();
var nr:Number;
nr=0;
for each (var eventInfo:XML in eventChildren) {
nr++;
trace(eventInfo.title);
var ev="ev"+String(nr);
var titl="title"+String(nr);
trace(ev);
trace(titl);
var newEV:Object = Object(ev);
var newTITL:Object = Object(titl);
trace(newEV);
trace(newTITL);
newEV.newTITL.text=eventInfo.title;
}
}
this is my code, i'm trying to set the title value for every child instance of eventChild,
as i am new to action script in general, and action script 3 in particular i don't really know what i'm doing wrong here. I'm trying to set the text for ev1.title1, ev2.title2, etc. from values in eventChildren like this : first child, sets ev1.title1, second ev2.title2 and so on. Any ideas on what i should change in the code or where to look for some information ?
edit : thank you for the help, both answers took me to the right solution :
for each (var eventInfo:XML in eventChildren) {
nr++;
trace(eventInfo.title);
var ev="ev"+String(nr);
var titl="title"+String(nr);
//trace(ev);
//trace(titl);
var oTitle:Object = {}; // create object for the field titleXX
oTitle[titl] = {text:eventInfo.title}; // create and assign the field text to a new object
allFields[ev] = oTitle; // assign the title object to the field evXX
}
ev1.title1.text=allFields.ev1.title1.text;
ev2.title2.text=allFields.ev2.title2.text;
ev3.title3.text = allFields.ev3.title3.text;
ev4.title4.text=allFields.ev4.title4.text;

ev and titl are Strings and not Object, there is no eval in as3 so you will not be able to create a new variable based on a string name .But you can create a new Object that will have a field based on your ev string:
var o:Object={};
o[ev]="....";
So if ev is equal to the string "ev1" you will have an object with a field named ev1 => o.ev1=...
For the title you can do the same create a new Object that will have a field based on titl string:
var o:Object={};
o[titl]="...";
So if titl is equal to the string "title1" you will have an object with a field named title1 => o.title1=...
Same thing for the text you have to create an Object to hold the text field.
Mixing all this infos you end up with:
var eventChildren:XMLList = eventInput.channel.children();
var nr:Number=0;
var AllFields:Object={};
for each (var eventInfo:XML in eventChildren) {
nr++;
trace(eventInfo.title);
var ev="ev"+String(nr);
var titl="title"+String(nr);
trace(ev);
trace(titl);
var oTitle:Object = {}; // create object for the field titleXX
oTitle[titl] = {text:eventInfo.title}; // create and assign the field text to a new object
allFields[ev] = oTitle; // assign the title object to the field evXX
}
// then you can have access to all your field within the object allFields
trace(allFields.ev1.title1.text)
trace(allFields.ev2.title2.text)
See also this question for object notation

You can create the variable name using 'this':
this['mystring'] = new Object();
this.mystring.title = 'mytitle';
If you're doing this inside a class, the class has to be dynamic to allow for new members:
dynamic public class MyClass extends MovieClip {
public function MyClass() {
this['mystring'] = new Object();
this.mystring.title = 'mytitle';
}
}
If your class is not dynamic, you can still do this but must continue to use array notation rather than dot notation:
public class MyClass extends MovieClip { // not dynamic
public function MyClass() {
this['mystring'] = new Object();
this['mystring'].title = 'mytitle';
}
}

Related

Convert and parse json string to key value pairs using NewtonSoft

Trying to convert a json string to key value pairs using Newtonsoft but no luck so far.
Response from the API:
var response = #"{'result':{'0199 - B344EE33':
{
'6400_00260100':{'1':[{'val':336688}]},
'6400_00462500':{'1':[{'val':null}]},
'6800_00832A00':{'1':[{'low':3000,'high':3000,'val':3000}]},
'6800_008AA200':{'1':[{'low':0,'high':null,'val':0}]}
}}}";
Result I want is a new object of key value pairs:
{
"6400_00260100" : 336688,
"6400_00462500" : null,
"6800_00832A00" : 3000,
"6800_008AA200" : 0
}
In the response the result will always be the first and only prop. In the next level the code 0199 - B344EE33 can change but there will be only one prop in this level so we can always take the first one. Then in the last level we always need the val property.
What I have is the following but for getting the key value pairs in a clean way I got stuck:
var json = JObject.Parse(response);
var result = json["result"].First;
var path = result.Path;
UPDATE
var jObjectResult = new JObject();
var response = #"{'result':{'0199 - B344EE33':
{
'6800_10821E00':{'1':[{'val':'SMA Sunny Boy'}]},
'6800_00A21E00':{'1':[{'val':'3.0.0.2222'}]},
'6800_00823400':{'1':[{'low':3000,'high':3000,'val':3000}]},
'6800_08822B00':{'1':[{'val':'SMA'}]},
'6800_08822000':{'1':[{'val':'Sunny Boy 3.0'}]}
}}}";
var json = JObject.Parse(response);
var json_serial = json["result"].First.Children<JObject>().ToList()[0];
foreach(var token in json_serial)
{
var tokenKey = token.Key;
var tokenVal = token.Value.SelectToken("$.1[0].val");
jObjectResult.Add(tokenKey, tokenVal);
}
You could use SelectTokens with the recursive descent operator .. to find all the val properties, then walk up the chain using .Parent repeatedly to get the corresponding key. Create new JProperties from this information and put them into a new JObject to get your result. Here is a "one-liner":
var result = new JObject(
JObject.Parse(response)
.SelectTokens("$..val")
.Select(jt => new JProperty(
((JProperty)jt.Parent.Parent.Parent.Parent.Parent.Parent).Name,
jt
))
);
Fiddle: https://dotnetfiddle.net/TbZ7LS
At the end with some pointers form #Brian Rogers I came with the following solution:
// Arrange
var response = #"{'result':{'0199 - B344EE33':
{
'6800_10821E00':{'1':[{'val':'SMA Sunny Boy'}]},
'6800_00A21E00':{'1':[{'val':'3.0.0.2222'}]},
'6800_00823400':{'1':[{'low':3000,'high':3000,'val':3000}]},
'6800_08822B00':{'1':[{'val':'SMA'}]},
'6800_08822000':{'1':[{'val':'Sunny Boy 3.0'}]}
}}}";
// Act
var json = JObject.Parse(response);
var json_serial = (JProperty)json["result"].First();
var jObjectResult = new JObject(
json_serial.Value.Select(p =>
{
return new JProperty(
((JProperty)p).Name,
p.First.SelectToken("$.1[0].val")
);
}));

Create the objectIds map with variable keys for a DuplicateObject request

When creating a "duplicate object" request, the objectIds map will not let me put a variable in the emphasized portion below:
var alternateSlideId = 'alternate_' + i;
{
duplicateObject:
{
objectId: alternateSlide,
objectIds: {
**alternateSlide**: 'Copied_Alternate_Slide_' + i,
}
}
}
(i is a number in a loop)
From my understanding the map function works like
((objectId to be copied) : (name of new objectId))
I'm unable to use a variable on the left side of the map function, and I'm unable to put 'alternate_' + i into the left side of the map and I'm uncertain as to why. I need to duplicate multiple slides that have already been duplicated before, and thus have variable names.
How can I assign variable keys to the objectIds map?
You need to specify unique ids. Not just unique to the slide, but the whole presentation. Consider using Utilities.getUuid() as I do in this answer.
Google Apps Script is essentially JavaScript 1.6, so to write to a a variable property name, you need to use the bracket operator, rather than the dot operator or the variable name / shorthand syntax in the object literal constructor. Your past attempts likely attempted to do this from the constructor:
Won't work (object literal constructor):
var v = "some prop name";
var myObj = {
v: "some prop value",
};
console.log(myObj); // {'v': 'some prop value'}
Won't work (dot operator):
var v = "some prop name";
var myObj = {};
myObj.v = "some prop value";
console.log(myObj); // {'v': 'some prop value'}
Won't work (since GAS is not ECMAScript2015 or newer), and throws "Invalid property ID" error when saving:
var v = "some prop name";
var myObj = {
[v]: "some prop value",
};
console.log(myObj);
Will work (bracket operator):
var v = "some prop name";
var myObj = {};
myObj[v] = "some prop value";
console.log(myObj); // {'some prop name': 'some prop value'}
Thus, your code to copy the a slide represented by the variable altSlide needs to be something like:
var newAltSlideId = ("copied_altSlide_" + i + "_" + Utilities.getUuid()).slice(0, 50);
var dupRqConfig = {
objectId: altSlide.objectId, // the object id of the source slide
objectIds: {} // map between existing objectIds on the altSlide, and the new ids
};
// Set the duplicate's ID
dupRqConfig.objectIds[altSlide.objectId] = newAltSlideId;
// If you want to set the objectIds in the duplicate, you need to
// loop over altSlide's child objects. An example:
altSlide.pageElements.forEach(function (child, index) {
dupRqConfig.objectIds[child.objectId] = /** some new id */;
});
requests.push({ duplicateObject: dupRqConfig }); // add the request to the batchUpdate list
References:
Object initializer
DuplicateObject Request
Page resource (aka a "slide")
"Copy a slide" sample
Array#forEach

Reverse engineering - Flash app

I have that code:
private function handleFlashVarsXmlLoaded(event:Event) : void
{
var secondsplit:String = null;
var item:Array = null;
var string:* = XML(String(event.target.data));
var notsplited:* = string.vars_CDATA; //what is .vars_CDATA?
var splitted:* = notsplitted.split("&");
var datacontainer:Object = {};
var index:Number = 0;
item = secondsplit.split("=");
datacontainer[item[0]] = item[1];
this.parseFlashVars(datacontainer); // go next
return;
}
That function is loaded when URLLoader is loaded.
I think that this function parse a XML file to string(fe. param1=arg1&param2=arg2), then split it by "&" and then by "=" and add data to datacontainer by
datacontainer["param1"] = "arg1"
But how should the XML file look like and what is string.vars_CDATA
I think, vars_CDATA is just a name of XML field, becourse variable named "string" is contains whole XML. So var "notsplited" contains a String-typed data of this field (I think so, becourse of the line "var splitted:* = notsplitted.split("&");", which splits String to Array).

Return ArrayCollection from SELECT query

In my AIR app i am trying to get the names from sqlite database as an ArrayCollection . Here is my code.
private function visitorName():void {
var sqlText:String = "SELECT name FROM user";
visitorNames = new SQLStatement;
visitorNames.sqlConnection = dbConn;
visitorNames.addEventListener(SQLEvent.RESULT, visitornamesResult);
visitorNames.addEventListener(SQLErrorEvent.ERROR, errorHandler);
visitorNames.text = sqlText;
visitorNames.execute();
}
private function visitornamesResult(event:SQLEvent):Array {
var result:SQLResult = visitorNames.getResult();
var namesList:Array = new Array();
namesList = result.data;
datafield3.dataProvider = namesList;
return namesList;
}
What should i do to get the results to an ArrayCollection by calling the visitorName() function?
Is it possible to get return value from a nested function?I know the visitorName function should be changed to ArrayCollection type and should declare an ArrayCollection variable inside it .. but not so sure how to proceed .. any help appreciated ..
First of all, notice that visitornamesResult is an event handler, so you won't be able to get the return value of that function.
You will want to actually put the result somewhere when you get it (in the UI? or in a model?)
Let's keep it simple, and just assign it to your datafield3.dataProvider:
private function visitorName(): { ... /* same as before */ }
private function visitornamesResult(event:SQLEvent):void {
var result:SQLResult = visitorNames.getResult();
// this is how you create an ArrayCollection with a provided Array
var visitors: ArrayCollection = new ArrayCollection( result.data );
datafield3.dataProvider = visitors;
}

How to know what class is referenced by a Class object?

We have the Class object (an object that reference a Class) so you can create objects from that Class object:
var classObject:Class = package.to.class.AClass;
var objectFromClass:AClass = new classObject();
Now, I want to know what object is referenced by classObject. For example:
function Creator(classObject:Class):AClass
{
// here I want to know what class is referenced by classObject
return new classObject();
}
var classObject:Class = package.to.class.AClass;
var objectFromClass:AClass = Creator(classObject);
This works, but what if I pass a Class object that do not reference to AClass? I want to know if this happends and make somthing about it.
--- EDIT ---
Searching I found this function
flash.utils.getQualifiedClassName(value:*):String
This function returns the name of the class, for example:
var name:String = '';
// name = ''
name = flash.utils.getQualifiedClassName(package.to.class.AClass);
// name = 'AClass'
name = ''
// name = ''
var anInstance:AClass = new AClass();
name = flash.utils.getQualifiedClassName(anInstance);
// name = 'AClass'
So, all I have to do is to compare the results of that function:
function Creator(classObject:Class):AClass
{
var anInstance:AClass = new AClass();
var className:String = flash.utils.getQualifiedClassName(anInstance);
var classObjectName:String = flash.utils.getQualifiedClassName(classObject);
// here className and classObjectName are 'AClass' :)
if (className != classObjectName)
throw new Error('The classes are different');
return new classObject();
}
var classObject:Class = package.to.class.AClass;
var objectFromClass:AClass = Creator(classObject);
--- EDIT 2 ---
Another method is to use the constructor property of the Object class:
function Creator(classObject:Class):AClass
{
var tempInstance:AClass = new AClass();
var tempClassObject:Class = Object(tempInstance).constructor;
if (classObject != tempClassObject)
throw new Error('The classes are different');
return new classObject();
}
I found that the most simplest (not know if it's the fastest) way to accomplish this task is in the next example:
function Creator(classObject:Class):AClass
{
var anInstance:Object = new classObject() as AClass;
if (anInstance == null)
throw new Error('The classes are different');
return new classObject(); // or return anInstance as AClass;
}
This also works if AClass is an Interface.