new constructor with a string - actionscript-3

Instead of many if conditionals, I want to call a constructor according to a string value
var valueString:String = "myNewClassB";
var value:Class = valueString as Class;
new value() // new value() == new myNewClassB()
I know it's gonna fail, I need help. Thanks.

var ClassReference:Class = getDefinitionByName("myNewClassB");
var instance = new ClassReference();
That's the basics, bud.

If you want to do that, there are two ways, either assign classes to a list of classes made for an example in a object:
var list:Object = {
classA: FirstClass,
classB: SecondClass,
classC: ThirdClass
}
and than call them by a string:
var desiredObject:* = new (list["classA"] as Class)();
or you could also use getDefinitionBtName but than if you want to get a class you need to provide a full name (with the package)
var desiredClass = getDefinitionByName( "com.somedomain.SomeClass" );
If you are laoding an SWF content and than want to get a class from it you should use that loader loaderInfo.applicationDomain.getDefinition( "....class" );
you can also check if a class is defined by:
loaderInfo.applicationDomain.hasDefinition( "....class" );
link: ApplicationDomain.getDefinition
link: ApplicationDomain.hasDefinition
link: LoaderInfo

Related

How to make Json.stringify ignore certain class memebers?

I'm using the latest Haxe and HaxeFlixel to make a simple game prototype.
I have the following class...
class GameData
{
public var playerHealth: Int;
public var playerScore: Int;
public var levelName: String;
public function new(playerHealth: Int = 0, playerScore: Int = 0, levelName: String = "")
{
this.playerHealth = playerHealth;
this.playerScore = playerScore;
this.levelName = levelName;
}
}
I convert it to JSON as follows...
Json.stringify(new GameData(64, 512, "Level 1"));
Is there's a way I can make it so the stringify ignores certain members?
haxe.Json has no mechanism to exclude fields, so I would recommend using a third-party library such as json2object that does. Here you can simply annotate fields that should be ignored with #:jignored:
#:jignored
public var levelName:String;
var data = new GameData(100, 10, "Level 1");
var json = new json2object.JsonWriter<GameData>().write(data);
trace(json); // {"playerHealth": 100,"playerScore": 10}
There are some possible workarounds that don't involve adding a library to your project, but they don't seem very nice:
Don't serialize the object directly, but a structure that only includes the desired fields:
var data = new GameData(100, 10, "Level 1");
var json = Json.stringify({
playerHealth: data.playerHealth,
playerScore: data.playerScore
});
trace(json); // {"playerHealth":100,"playerScore":10}
Remove the unwanted fields after serialization - this seems rather hacky as it involves a lot of unnecessary overhead due to an additional Json.parse() and Json.stringify() call:
var json = Json.stringify(new GameData(100, 10, "Level 1"));
var data:haxe.DynamicAccess<String> = Json.parse(json);
data.remove("levelName");
json = Json.stringify(data);
trace(json); // {"playerHealth":100,"playerScore":10}
Depending on your exact situation, it can be desirable to make a slightly modified version of standard library's JsonPrinter - for example, in GMEdit I allow JSON objects to have an hxOrder: Array<String> field, which, if provided, determines the field order for printing, and is initialized to a static array. You can make a similar scheme for field inclusion/exclusion.

Create json object in AngularJS

I am trying to create a json object in AngularJS
My code is as below
for(i=0;i<5;i++){
var dCol = file.columns[i].value;
var dRow = file.rows[i].value;
$scope.fileContents.push({ dCol: dRow });
}
But the value of dCol is not reflecting. It is taking dCol as a string "dCol".
Can someone please help me out. I am fairly new to angularJS. I would really appreciate your help.
Instead of doing this:
$scope.fileContents.push({ dCol: dRow });
please do this:
var tmpObj = {};
tmpObj[dCol] = dRow;
$scope.fileContents.push(tmpObj);
Are youa bit confused with the differences between variables and object properties,
in your example you are adding a property named dCol to the object pushed into fileContents...
by the way,
if the dCol variable is a string you can do something like that:
var item = {}; item[dcol] = dRow; fileContents.push(item);
if the variable dCol isn't a string, you may consider to add it to the object:
fileContents.push({ "dCol": dCol, "dRow": dRow });
that's because dCol is a key and a key is always a string in a JS object;
try this:
for(i=0;i<5;i++){
var obj ={};
var dCol = file.columns[i].value;
var dRow = file.rows[i].value;
obj[dCol]=dRow;
$scope.fileContents.push(obj);
}
if you like a more synthetic approach , this should work to:
$scope.fileContents.push({ [dCol]: dRow });

Is is possible to pass a string object inside another object?

I have a string and i would like to pass it by reference in a object.
var str:String = '';
var object:Object = {s:str};
object.s = 'test';
trace(str); // i would like this to output test but it is just blank
trace(object.s); //this has a value stored in it
Is this possible or am i thinking about this wrong?
What i am trying to do is update the str value by updating object.s. I want the sting object to be inside the object not the value of the str.
I want it to do something more like this
var obj2:Object = new Object();
obj2.txt = '';
var object:Object = {s:obj2};
object.s.txt = 'test';
trace(obj2.txt); // outputs test
trace(object.s.txt); //outputs test
Of course it is possible. In the above example i suspect the trace works fine, but since there is no value, it does not display anything. I don't know actionscript very well, but since it compiles down to javascript then all objects are passed by reference. your example code actually has two objects in it one called string that is a string with no value assigned, and the other an object with a property called s that is a string. that it why there is no output.
var string:String = '';
var object:Object = {s:string};
object.s = 'test';
string = object.s;
trace(string); // i would like this to output test but it is just blank
trace(object.s);
This should show you what you want to see, and is passed by reference, unless actionscript enforces pass by value, which i do not believe it does

loop through complex xml nodes actionscript

I am reading an xml file in actionscript.
I need to loop through each node named "entry" as showing in below image :-
Can anyone help me ?
I am trying below code. but it not working :-
var categoryList:XMLList = x.feed.#entry;
for each(var category:XML in categoryList)
{
trace(category.#name);
for each(var item:XML in category.item)
{
trace(" "+item.#name +": "+ item);
}
}
"entry" node also has some inner nodes, I also want to read those nodes.
Thanks
This XML is using the namespace http://www.w3.org/2005/Atom, so you have to account for that:
var n:Namespace = new Namespace("http://www.w3.org/2005/Atom");
var categoryList:XMLList = x.n::entry;
Update:
In order to access child nodes, you will need to continue to use the namespace
for each(var category:XML in categoryList)
{
// this traces the name of the author
trace(category.n::author.n::name.toString());
}
Better is:
var n:Namespace = new Namespace("http://www.w3.org/2005/Atom");
default xml namespace = n;
var categoryList:XMLList = x.entry;//no namespace type access
//etc
default xml namespace = null;
Change declaration of categoryList to:
var categoryList:XMLList = x.entry;
It should loop through entry nodes now.

How to use a variable as part of an URL

I have a variable
var qstAccessCode:String = "default";
and a loader with URLRequest
var qst:XML;
var qstLoader:URLLoader = new URLLoader();
qstLoader.load(new URLRequest("http://dl.dropbox.com/u/44181313/Qaaps/Audio/" + qstAccessCode + ".qst"));
qstLoader.addEventListener(Event.COMPLETE, processQST);
function processQST(e:Event):void {
qst = new XML(e.target.data);
trace("QST loading");
}
I would like to use the value of qstAccessCode to complete the URL (so I can change the URL based on user input - if no input then use "default") but I get an error:
"1120: Access of undefined property qstAccessCode"
Is this to do with scoping? How can I complete the URL? Thanks in advance.
Edit: I haven't been able to get clear on this, so I'm also going to look at generating the complete URL from the user-input function and see if I get the URLRequest to pick it up as a variable. If there are any further comments on the original idea I will be very grateful to read them. Cheers.
Edit: #Moorthy I have qstAccessCode defined like this:
var qatAccessCode:String = "default";
var stageText:StageText = new StageText();
stageText.returnKeyLabel = ReturnKeyLabel.GO;
stageText.stage = this.stage;
stageText.viewPort = new Rectangle(225, 765, 200, 35 );
stageText.addEventListener(Event.CHANGE, onChange);
function onChange(e:Event):void
{
qatAccessCode = stageText.text;
trace(qatAccessCode);
}
It traces keyboard entry when I test movie (Air 3.2 for Android).
qstAccessCode should be defined in the same scope as the URLRequest.
You must defined property qstAccessCode like:
var qstAccessCode:string;
qstAccessCode's value is your url address.