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.
Related
CODE :
var data={"today date":'12-02-2014' ,"created date":'10-2-1014'}
tpl= new Ext.XTemplate('<tpl for=".">','<p>{today date}</p>','<p>{created date}</p>','</tpl>');
here I have attached the sample code.
Simple answer: don't.
var data = {"today date":'12-02-2014' ,"created date":'10-2-1014'};
// notice udpated variable names
var tpl = new Ext.XTemplate('<tpl for=".">','<p>{today_date}</p>','<p>{created_date}</p>','</tpl>');
var fixedData = {};
Ext.each(Ext.Object.getKeys(data), function(key) {
fixedData[key.replace(/ /g, '_')] = data[key];
});
// test
alert(tpl.apply(fixedData));
I have two textbox(goalText and goalText1) and a button(goalreach) in my html.
My aim : When I enter numeric value in 1 textbox(goalText), it should be converted to json and be stored. So even after 5 days when I run the application, it should be stored. Now when I enter the numeric value, in other textbox(goalText1) and it matches, I am simply displaying the message match. This is the demo, I am trying so that I can know that value can be stored in json and can be retrieved when necessary. I have written the code as follow:
$("#goalreach").click(function () {
var contact = new Object();
contact.goalDist = "$("#goalText.value ").val()";
var jsonText = JSON.stringify(contact);
if (jsonText == ($("#goalText1.value").val())) {
document.getElementById('divid').innerHTML = 'Match';
}
});
I know, I have made many simple mistakes of brackets and " too, but I am a newbie, If you can help me out.
First, you have to compare either 2 objects or 2 strings, and in goalDist, you should store the value (BTW, you get the jQuery object with $("#goalText") and the value with somejQueryObject.val() moreover this is generally equivalent to document.getElementById("goalText").value)...
This can be done like this :
$("#goalreach").click(function () {
// Create an object with the single property "goalDist"
var contact = { goalDist : $("#goalText").val() };
// Makes it be a string (it will in this simple example : `"{"goalDist":<the value of goalTest>}"`
var jsonText = JSON.stringify(contact);
// Creates a string from an equivalent object bound on the second field
var jsonText2 = JSON.stringify({ goalDist : $("#goalText2").val() });
// Compares the 2 strings
if (jsonText === jsonText2) {
document.getElementById('divid').innerHTML = 'Match';
}
});
TRY THIS
$("#goalreach").click(function () {
var contact = new Object();
var goalDist = '$("#goalText.value").val()';
var jsonText = JSON.stringify(contact.goalDist);
if(jsonText==($("#goalText1.value").val()))
{
document.getElementById('divid').innerHTML = 'Match';
}
});
Try the following code:
$("#goalreach").click(function () {
var contact = new Object();
contact.goalDist = $("#goalText").val();
var jsonText = JSON.stringify(contact);
if (jsonText == ($("#goalText1").val())) {
document.getElementById('divid').innerHTML = 'Match';
}
});
OR
$("#goalreach").click(function () {
var goalText = $("#goalText").val();
var goalText1 = $("#goalText1").val();
if (goalText == goalText1) {
document.getElementById('divid').innerHTML = 'Match';
}
});
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¶m2=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).
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;
}
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';
}
}