I created a database (scriptDB) and have stored information (more than 40 properties of about 600 people-students-).
var obj = {
alumne_id: email,
alumne_ordre: nomsencer_ordre,
alumne_timestamp: {created: new Date (). getTime (), changed:'', editor: 'unedited'},
alumne_nom: {nom: nomREAD, cognom1: cognom1READ, cognom2: cognom2READ, cognoms: cognomsREAD, nomsencer: nomsencerREAD},
(...)
}
I stored this object in my database:
var stored = db.save(ob);
I defined thus, properties of objects and have my stored. This works perfectly and update data, for example, change "alumne_nom.nom" to a new value: 'Joana'
Now I want to add new properties to all or some of these objects (students).
The problem is that I can add a property as
newproperty1
with:
stored.newproperty1 = '50 '
But I can not do:
stored.newproperty1.sub1
stored.newproperty1.sub2
stored.newproperty1.sub3
Does anyone know how I can add these subproperties?
To add "subproperties" you need to first define the property. You can do it by setting an empty object to it, e.g.
stored.newproperty1 = {};
//then all subs should work
stored.newproperty1.sub1 = 10;
You could already pass the subs definitions on the braces as you do with the object as well:
stored.newproperty1 = {sub1:10, sub2:20, etc:'value'};
Related
I have created multiple instances of one object each with there own instance names with numbers on the end. I would like to iterate through these names (they have numbers on the end), modify there properties then push them to an array all through the instance names not the actual object instances.
if (instance60.currentFrame == 4)
{
array.push("instance60")
}
Where the first instance60 is refering to the object itself and the second instance60 is just a pushed string
I think what you are looking to do is set a key value pair.
The key would be how you find the reference to the object later
instances["instance"+60] = instance60;
you can loop using something like:
for(var i:uint=0;i<instances.length;i++){
instances["instance"+i] = new Sprite();
instances["instance"+i].x = 20;
instances["instance"+i].y = 20*i;
//etc...
}
But, although this has it's uses, in this case you are iterating through an integer value anyways, so, given it can run quite a bit faster on large arrays, why not just store them at their position in the array?
for(var i:uint=0;i<instances.length;i++){
instances[i] = new Sprite();
instances[i].x = 20;
instances[i].y = 20*i;
this.addChild(instance[i]);
//etc...
}
I've spent nearly 1 week to learn working with objects instead of arrays. I had thought it was easy to call them and created some objects and set their properties. However I can't access them now, I tried this:
function onBoxClick(event:MouseEvent):void {
var str:String = event.currentTarget.name;
trace(str);
str = str.substring(str.indexOf("_") + 1);
trace(getChildByName("copy_" + str)); // trying to trace an object by name
}
My question is if there's a practical way of dealing with objects, otherwise what's the purpose of using them.
Edit: Here's my function that I use to create movieclips and other things:
function addBoxes(isUpdate:Boolean):void {
var copyOne:Object = getReadOnlyValues();
copyOne.name = "copy_" + num;
// Set default mc1 settings
var settings1:Object = copyOne.mc1Settings;
for(var num2:String in settings1) {
copyOne.mc1[num2] = settings1[num2];
}
// Set default mc1text settings
var settings2:Object = copyOne.mc1TextSettings;
for(var num3:String in settings2) {
copyOne.mc1Text[num3] = settings2[num3];
}
copyOne.mc1.x = nextXpos;
copyOne.mc1.name = "captionBox_" + num;
addChild(copyOne.mc1);
copyOne.mc1.addEventListener(MouseEvent.CLICK, onCaptionClick);
copyOne.mc1Text.name = "captionBoxText_" + num;
copyOne.mc1.addChild(copyOne.mc1Text);
// ---------------------------------------------------------------
// Set default mc2 settings
var settings4:Object = copyOne.mc2Settings;
for(var num4:String in settings4) {
copyOne.mc2[num4] = settings4[num4];
}
// Set default mc2text settings
var settings5:Object = copyOne.mc2TextSettings;
for(var num5:String in settings5) {
copyOne.mc2Text[num5] = settings5[num5];
}
copyOne.mc2.x = nextXpos;
copyOne.mc2.y = copyOne.mc1.height;
copyOne.mc2.name = "box2_" + num;
addChild(copyOne.mc2);
copyOne.mc2Text.name = "box2BoxText_" + num;
copyOne.mc2.addChild(copyOne.mc2Text);
copyOne.mc2.addEventListener(MouseEvent.CLICK, onBoxClick);
if (num / subunits is int) {
trace (num);
// createMc("normalBox", true);
}
nextXpos = nextXpos + copyOne.mc2.width;
// traceObj(copyOne);
// traceObj(getReadOnlyValues());
}
I called this function in a loop so I created many movieclips. Now I can't access objects' properties and their childen (e.g textfield).
Objects I have on stage: Movieclips and textfields
Where they come from: The function above
What I'm trying to do with them: Tracing movieclips and textfields (that are holded by objects) to change their children (textfield) text
What happens instead of what I expect: Trace code outputs undefined instead of giving me object type trace(getChildByName("copy_" + str)); // trying to trace an object by name
Is there a practical way of accessing an object whose name is "copy_1" and its property whose name is "box2_1" (movieclip)?
One problem I see is the "copyOne" object has been created within the scope of "addBoxes", so it will no longer exist outside of this function.
Another is you're trying to access an Object via getChildByName, which only addresses displayObjects of the displayObjectContainer you are calling from.
If you want to loosely keep track of variables with things like Objects or MovieClips (which are both dynamic-style objects that let you add properties to them as you wish), just use MovieClips to house your values. The movieClips, being on the stage, will be retained in memory until removed from the displayList (stage).
Also, check out the Dictionary, a sort of key/value based way of storing collections of objects.
Better yet, if you use strongly-typed custom objects (creating your own classes to extend MCs, and adding your own public or private methods and values), there are benefits such as using Vectors (fancy, fast arrays that are compatible with any Object type you choose).
I don't really know if I understood your question or not, but as #ozmachine said in his answer, you can not use getChildByName, instead I think that you can take a look on this, may be it can help :
var container:DisplayObjectContainer = this;
function getReadOnlyValues():Object {
return {
mc1: new box(),
mc1: {
name: 'mc1_',
alpha: 1,
x: 0,
y: 0,
width: 30,
height: 25
},
mc1Text: new TextField(),
mc1Text: {
text: 'test',
x: 0,
y: 0,
selectable: false,
multiline: false,
wordWrap: false
}
}
};
// create 5 objects
for(var i=0; i<5; i++){
container['copy_'+i] = getReadOnlyValues();
var obj:Object = getObjectByName('copy_'+i);
obj.mc1.alpha = 1;
obj.mc1.x = 0;
obj.mc1.y = 50 * i;
obj.mc1.width = 100;
obj.mc1.addChild(obj.mc1Text);
obj.mc1Text.text = 'test_' + i;
addChild(obj.mc1);
}
// get object by name
function getObjectByName(name:String):Object {
return container[name];
}
// change the text of the 4th button
stage.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
var obj:Object = getObjectByName('copy_3');
obj.mc1Text.text = 'new text';
})
Array and Object are both data structures.
Data means some form of information.
Data structure means some form of information being stored in a certain way.
Array and Object are two different ways to store information.
Arrays identify data with integer numbers.
An integer number to identify a single element of an array is called an index
Arrays are ideal to represent a list of similar things that belong to each other.
var names:Array = ["John", "Paul", "George", "Ringo"];
This often means that the elements of an array are of the same type, like in the example above.
But they don't have to:
var numbers:Array = [42, "twenty-five", "XIIV"];
For the above examples it's easy to answer the questions "What are the names of the four beatles?", "What different representations of numbers did you stumble upon during your trip through the historic town?". Other questions are harder or impossible to answer. "What Roman numerals did you stumble upon in the historic town?"
Objects identify data with names.
A name to identify a single element of an object is called a property
Objects are ideal to represent a list of dissimilar things that belong to each other.
var paula:Object = {age:37, name:"Paula", hairColor:0x123456};
This often means that the elements of an object are of different type, like in the example above.
But they don't have to:
var car:Object = {manufacturer:"Porsche", color:"red", sound:"wroooooom", soundOfDriver:"weeeeeeeeeeee"};
Considering this, let's take a look at your code and see how it applies.
The big picture is that you have a function addBoxes that you call multiple times. As one function should have one purpose, this function will do something similar every time it is executed. Uh-Oh: "similar". Whatever the result of this function is, it should go into an array. Each call to that function would be an element of the array. You can see this clearly on your use of "num" to identify whatever is happening in your current run of the function.
What data is present in your function?
copyOne
mc1
mc1Text
mc2
mc2Text
copyOne is a troublemaker here and what causes your confusion. It's trying to do everything at once and therefore you are not able to think clearly about when to use a Array and when Object. One would call it a god object. And that's not a good object to have around.
Your choice for variable names is very bad.
You choose super generic names like "mcX" only to later add a name property to it that describes what it truly is.
But even that doesn't hold true for whatever "Box2" is supposed to be.
Choose names so that it'S easy to understand what something in your code is.
It looks like you created all or parts of this structure jsut for this question and therefore lacked meaningful names.
I highly recommend that you do not learn by such made up projects. But from the real world.
I will therefore impose the following goal:
mc1 and mc1Text represent a caption
mc2 and mc2Text represent a content
With all this, I ask again:
What data is present in your function?
captionBox
captionText
contentBox
contentText
Both caption and content consist of a box and a text.
These are different things, so caption and content are each an object with properties "box" and "text"
One could think that due to this similarity, they both should go into an array.
But I beg to differ. A caption and a text are not the same thing. You deal with captions and texts differently. Walking on the streets you might catch a big caption in the news quickly, but not a lengthy text. That's why each of them should be a property of the object that's created in the function.
Here's somewhat of a conclusion:
var allBoxes:Array = []; // array to store the similar results of every function call
function createBoxes():void
{
var boxes:Object = {};
//the box consists of caption & content, both bying of the same type, but are containing different data
boxes.caption = {box:{}, text:{}}; //caption
boxes.content = {box:{}, text:{}}; //content
allBoxes.push(boxes);
}
This is it. That's how and why I would model your data with objects and arrays.
But it doesn't end here. My conclusion lacks a lot of the code you posted.
While the above is mostly language independent, the missing code is specific to Actionscript and not just on how to model data. It's as follows...
As3 is object oriented.
This is good, because the above conclusion has a lot of objects in it.
To define how some object is/does/moves/farts/etc, one creates classes.
The following changes take place (for reasons out of the scope of this answer):
createBoxes (formerly known as addBoxes) calls the constructor of
a class "CaptionAndContent" that extends Sprite.
There's no more need to explicitely create an object "boxes" as the constructor does exactly that.
The caption and content will not have a property "box", because
they can be the box themselves. This is exactly how it's done in the
code of the question. The default settings are set in the constructors of their classes.
Here's reduced snippet of code that hopefully illustrates how the classes could look like.
Each class should be in its own file, with the necessary imports, package block and the additional functionality that you did not specify in your question.
public class CaptionAndContent extends Sprite
{
private var caption:Caption;
private var content:Content;
public function CaptionAndContent(captionText:String = "", contentText:String = "")
{
caption = new Caption(captionText);
addChild(caption);
content = new Content(contentText);
content.y = caption.height;
addChild(content);
}
}
public class ClickableBoxWithText extends Sprite
{
protected var textField:TextField;
public function ClickableBoxWithText(text:String = "")
{
textField = new TextField();
textField.text = text;
addChild(textField);
addEventListener(MouseEvent.CLICK, onClick);
}
protected function onClick(mouseEvent:MouseEvent):void
{
//override this in a sublclass
}
}
public class Caption extends ClickableBoxWithText
{
public function Caption(text:String = "")
{
super(text);
// apply all the default settings of caption here.
}
}
public class Content extends ClickableBoxWithText
{
public function Content(text:String = "")
{
super(text);
// apply all the default settings of content here.
}
}
Using them would look something like this:
var allBoxes:Array = []; // array to store the similar results of every function call
function createBoxes():void
{
var captionAndContent:CaptionAndContent = new CaptionAndContent("This is the caption...", "...for this content");
captionAndContent.x = nextXpos;
addChild(captionAndContent);
allBoxes.push(captionAndContent);
}
Last but not least, the identification problem in the click handler.
Your question already contains the answer:
event.currentTarget
That's the reference to the object that was clicked on.
In my code it would be
mouseEvent.currentTarget
This identifies the object already. It's pointless to look up one of its properties (its name for example) in order to search all the objects for that name, just to identify the same object that you already had to identify (without a name) in order to get the name.
You aren't identifying the objects by name anyway. What differs between the names and what supposedly makes them unique is a number at their end. As pointed out in this answer, this is what's called an index and the thing you are trying to identify with it should go into an array. In my example codes, this is allBoxes.
I want to remove the first four indexes from the array using splice(), then rebuild the array starting at index 0. How do I do this?
Array.index[0] = 'one';
Array.index[1] = 'two';
Array.index[2] = 'three';
Array.index[3] = 'four';
Array.index[4] = 'five';
Array.index[5] = 'six';
Array.index[6] = 'seven';
Array.index[7] = 'eight';
Array.splice(0, 4);
Array.index[0] = 'five';
Array.index[1] = 'six';
Array.index[2] = 'seven';
Array.index[3] = 'eight';
I am accessing the array via a timer, on each iteration I want to remove the first four indexes of the array. I assumed splice() would remove the indexes then rebuild the array starting at 0 index. it doesn't, so instead what I have done is created a 'deleteIndex' variable, on each iteration a +4 is added to deleteIndex.
var deleteIndex:int = 4;
function updateTimer(event:TimerEvent):void
{
Array.splice(0,deleteIndex);
deleteIndex = deleteIndex + 4;
}
What type of object is "Array" in the code you have shown? The Flash Array object does not have a property named "index". The Array class is dynamic, which means that it let's you add random properties to it at run time (which seems to be what you are doing).
In any case, if you are using the standard Flash Array class, it's splice() method updates the array indexes automatically. Here is a code example that proves it:
var a:Array = [1,2,3,4,5];
trace("third element: ", a[2]); // output: 3
a.splice(2,1); // delete 3rd element
trace(a); // output: 1,2,4,5
trace(a.length); // ouput: 4
trace("third element: ", a[2]); // output: 4
If I am understanding what you want correctly, you need to use the unshift method of Array.
example :
var someArray:Array = new Array(0,1,2,3,4,5,6,7,8);
someArray.splice(0,4);
somearray.unshift(5,6,7,8);
Also, you are using the Array Class improperly, you need to create an instance of an array to work with first.
The question is confusing because you used Array class name instead of an instance of an array. But as the commenter on this post said, if you splice elements, it automatically re-indexes.
im not sure what you want to do, but Array=Array.splice(0,4) should fix somethin..
Using EWS managed api v1.1, I can successfully save/set the contact "Title" or honorific (if you prefer) to a non-empty value, but I can't figure out how to remove or set it back to an empty string/null.
I've tried to set an empty value and I've tried to remove the extended property. Here is relevant code.
var titleDef = new ExtendedPropertyDefinition(0x3A45, MapiPropertyType.String);
// works when set to a non-empty string value
ewsContact.SetExtendedProperty(titleDef, "Mr.");
// throws null argument exception when set to String.Empty or null
ewsContact.SetExtendedProperty(propDefinition, String.Empty);
// isRemoved is equal to false and the value doesn't change
var isRemoved = ewsContact.RemoveExtendedProperty(titleDef);
I've also tried to use a different overload on the ExtendedPropertyDefinition as mentioned in this very similar question, but it didn't change my end result for removing the property. I'm not sure I understand the difference in the two signatures for the constructor.
var titleDef = new ExtendedPropertyDefinition(new Guid("{00062004-0000-0000-C000-000000000046}"), 0x3A45, MapiPropertyType.String);
// isRemoved is equal to false and the value doesn't change
var isRemoved = ewsContact.RemoveExtendedProperty(titleDef);
Brute Force Work-Around
I suppose I could take a complete copy of the contact (without the title) and delete the original, but that seems a bit over the top and would probably cause other bugs.
EWS lets you assign Extended Properties without first binding them. However, to remove an Extended Property - you need to include it in your initial binding call PropertySet. The following worked for me...
var titleDef = new ExtendedPropertyDefinition(0x3A45, MapiPropertyType.String);
Contact contact = Contact.Bind(service, id, new PropertySet(titleDef));
contact.RemoveExtendedProperty(titleDef);
contact.Update(ConflictResolutionMode.AutoResolve);
It is also strange that you can retrieve the Title as a first-class property, but you cannot assign it (since it's a complex type). They could have made this easier for us.
var title = contact.CompleteName.Title;
I have a string like this:
var tempSting:String = "abc#abc.com;xyz#xyz.com"
I want to add this String into the ArrayCollection. And the above String should be divided by mail id and remove the ; symbol and need to add asArrayCollection
tempAc:Arraycollection = new ArrayCollection{abc#abc.com, xyz#xyz.com}
Please help me to add the split String into the ArrayCollection.
var tempString:String="abc#abc.com;xyz#xyz.com";
var tempArray:Array=tempString.split(";");
//tempAc is a predefined and presumably prepopulated arraycollection
for each(var email:String in tempArray) {
tempAc.addItem(email);
}
EDIT Now that I see Shane's answer I must add the following:
This code will append the array to the arraycollection. If you just want to create a new arraycollection, all you need to do is:
var tempAc:ArrayCollection=new ArrayCollection(tempArray);
or in 1 line,
var tempAc:ArrayCollection=new ArrayCollection(tempString.split(";"));
UPDATE - to answer questions in the comments:
tempAc.getItemAt(i) will give you the email id at the i th position
tempAc.getItemIndex("someone#email.com") will give you the index at which someone#email.com exists in the arraycollection (or -1 if not contained)
tempAc.contains("someone#email.com") will return true or false depending on if the string is contained or not in the arraycollection
So, to check for duplicate ids, all you got to do is:
var newEmailId:String="someone#email.com";
if(!tempAc.contains(newEmailId)) {
tempAc.addItem(newEmailId);
}
var tempString:String = "abc#abc.com;xyz#xyz.com";
tempAC:ArrayCollection = new ArrayCollection(tempString.split(";"));