As3 - Assign class to object on stage - actionscript-3

I think the title should make it pretty clear. I was wondering how you can assign a class to an object on stage. As you would do with actionscript:
var objectname:ClassName = new ClassName();
This would make a new object, but the current object already exists, it just needs to be notified that it's of the type "ClassName" so it can inherit it's properties.
I've also tried assigning the "ClassName" in the linkage as either the base path or the class name. But in either situations I get an error saying that the class needs to be unique when I use the same class on multiple objects.
So I would need something like
//example exists on stage
example.class = ClassName
Thanks

I will answer your question with a question : why are you assigning the same class on multiple objects?
If what you want is a common behavior for those objects, you should create your class and assign it has the Base Class on those objects.

I don't think there's a way to do just do that. But I do suggest you look into the decorator design pattern The idea here is that you don't change the class, but you "decorate it" with more functions as needed.
Hope this helps !

You seem to have this the wrong way around. You define a class in order to set specific behavior & properties for an object. In a real life example, if I want to build a radio , I will come up with a radio design & implement it. Now if I need several radios, I will use the same implementation to manufacture them.
If I now wish to turn my radio into a TV , I can't just tell my radio, hey , you're a TV now. I can either decide beforehand that I want a radio/tv object and switch behavior whenever necessary or I can create a new TV object and add the radio functionality to it by adding a radio component to my TV object.
var radio:Radio // your current object
//example 1
radio.switchToTv();
//example 2
var radioTv:Tv = new Tv( radio );

Related

Angular - Two Way Binding

I am a beginner at Angular and I covering two way binding but for some reason I do not understand what I am doing wrong with the below any input would be appreciated.
I am simply trying to understand the concept so the below code is rather simple. Per my understanding
Adding the two way binding [()] to <app-child-comp> I pass the parent field "name" from the parent component to the parent view and using property binding it provides an initialization value ( default value ) to the child component that receives the value in the #Input field.
Once the field "#Input childName " has its value using normal interpolation I can use the value how ever I please in the child template.
Now by defining an EventEmitter and then using its .emit method I should be able to pass any changes on the variable back up to the parent component and update the DOM property to reflect the changes.
Problem:
Now this is my problem the parent --> child direction the bindings are working as intended,
the name "Fin" is appearing as I expect in the input of the parent Template and in the interpolation in the child template but when I want to alter the name in the child template and have it bubble back up to the parent property it fails to update although it updates the interpolation in the child template.
Ive been trying to figure this out now for a while and everything im researching I feel says im doing it correctly but if you could please explain what im doing wrong I would much appreciate it.
Im adding this so that anyone looking in the future can learn from my mistake.
There are two ways to perform event binding given a child component
The first way is by explicitly declaring the property and event bindings as follows <app-child-comp [childName]="name" (childNameChange)="name =$event"></app-child-comp>
The second way is to use the "Banana Box" Method where the child tag transforms into <app-child-comp [(childName)]="name"></app-child-comp>
I was trying to use the second method and something that wasn't immediately clear is that there is a naming convention when it comes to the field names in the child component that needs to be followed in order for the "Banana Method" to work
Rule: If your #Input field is named "x" then your #Output EventEmitter needs to be named "xChange" the "Change" is required as the second part of the name .
Syntax: inputName + Change
So in order to resolve my problem I needed to change the naming convention from
#Input() childName:string;
#Output() changedName = new EventEmitter<string>();
to
#Input() childName:string;
#Output() childNameChange = new EventEmitter<string>();
You have to add the output in your root component:
<app-child-app [(childName)]="name" (changedName)="name = $event"></app-child-app>

Angular - Setting value in input text box in another component

I am trying to set the value in an HTML input text box which is a part of ComponentA from the typescript code which is a part of ComponentB.
Taking a clue from this SO i tried doing:
(<HTMLInputElement>document.getElementById("name")).value = response.name;
But this is not working. Is there anything else i need to take care of?
EDIT: The element with Id "name" is in ComponentA and the above code that is trying to manipulate that element is in ComponentB
If you are trying to set the value of component1's textfield from the compoenent2 then you must have to use of ngModel i.e two way data binding. by providing component2 in the providers list you are able to access all the functions and variables of that component, then you can easily set your value. like this
suppose this is your component 2's value property
name:string = 'Pardeep Jain';
than you can access this in component like this-
<input type="text" [(ngModel)]='name'>
....
constructor(private delete1: Delete){
this.name = this.delete1.name;
}
Working Example
Also
(<HTMLInputElement>document.getElementById("name")).value = response.name;
is used to set the value of current template's field with id named as **name**
This is one of the cases when user interaction on one component ComponentA triggers an update on another component ComponentB.
This article describes multiple approaches, with example code, on how to pass information between components.
My personal favorite is the third approach mentioned in that article in which one of the component (say ComponentA) "listen" for update it is concerned about from any component (say ComponentB) via a service in between them, resulting in a loosely coupled components.
For more approaches here is another link.

AS3 Nullify Object through Reference

I have seem people asking this question, and getting an answer saying that this is impossible in AS3.
So I have a function called destroymc:
function destroymc(mcname:MovieClip):void{
mcname = null
}
This doesnot destroy the movieclip I want to destroy. Because it is passed by reference.
Is there really no way to make this work?
thanks.
Hm, as far as I know, to 'destroy' a MovieClip in AS3.0, you need to get rid of all references to it, and you need to remove it from the parent. Then the GC can collect it.
If you have an array of MovieClips, then something like this could work.
var mcs:Array;
// populate array
// to destroy the first one
mcs[0].parent.removeChild(mcs[0]); // remove the first element from its parent
mcs.shift(); // remove the first element in the array
And so if you want in in a function, you could pass the array to it
function destroymc(mcs:Array){
mcs[0].parent.removeChild(mcs[0]);
mcs.shift();
}
I'm using an array here because you need to use functions like this with dynamically generated MovieClips and entity managers and stuff. I hope this helps.

AS3 Databinding to Specific property at an index in ArrayCollection

I have a situation where I want to use databinding from an ArrayCollection to populate text fields in a Flex view.
The ArrayCollection is populated from an SQL Result object. I store the ArrayColelction in my model class using getters and setters like this:
private var _monthlyData:ArrayCollection;
public function set monthlyData(value:ArrayCollection):void{
_monthlyData = value;
}
[Bindable]
public function get monthlyData():ArrayCollection{
return _monthlyData;
}
I use the monthlyData as a dataprovider for a list etc which works fine. I also need to use properties at certain indexs in this collection as text field strings.
When the text field text properties are set I don’t neccesarily have the monthlyData arrayCollection populated yet.
The text fields are set in another outside class with has a singleton reference to this model so I set the fields like so at the moment:
textField.text = _model.monthlyData.getItemAt(3).Month;
I want to setup binding to the array collection instead of just using this assignment method so that when that item in the array is refreshed or the entire arrayCollection is populated or updated , it will update the textField text.
I’m having trouble getting the binding to work.
I’m using bindageTools at the moment but have been also using the built in as3 BindingUtils to little effect.
I can do the following which sets the initial text property correctly, but it wont update when the ArrayCollection changes:
Bind.fromProperty(_model.monthlyData.getItemAt(3),"Month").toProperty(textField, "text");
So if someone could please point me in the right direction as to which way is best to get the binding going in pure AS3 no MXML, I’d really appreciate it.
Cheers
Marco
From the code you provide, I can see that monthlyData is bindable, which is fine. I'll assume that _model is bindable too.
However the getItemAt() method is not bindable (it will not dispatch propertychange events when items change positions in the collection), hence the text property of the text field will not be updated.
What you can do is something like this:
[Bindable]
public var selectedDate3:MyDate;
<s:TextInput id="myTextInput" text="{selectedDate3.month}" />
or the AS equivalent (why you want to make things hard on yourself is beyond me though)
BindingUtils.bindProperty(myTextInput, "text", selectedDate3, "month");
and then programmatically update selectedDate3:
_model.monthlyData.addEventListener(CollectionEvent.COLLECTION_CHANGE, updateSelected);
private function updateSelected(event:CollectionEvent):void {
selectedDate3 = _model.monthlyData.getItemAt(3);
}
Note that the month property of MyDate must also be bindable.
You mention that these fields are in a VGroup. I'm guessing you want to display a top 3 or something. This is still a list. It would be much easier and cleaner to do this with a List or DataGroup and simply filter the ArrayCollection to only display the first 3 items (or whatever rule for the items to be displayed), or configure the DataGroup to display only three items (it has no scrollbar anyway).

Ontology: OWL - Creating connections between classes

I ve got an Ontology written in OWL with Protege. But I don't find a solution for creating relations between Classes. Of course, there is a "subclass" relation, but I want to define my own relations. So I have a class hierarchy (which consists out of "subclass"-relations) but I want to create a relation, i.e. "has_Relation", to connect two classes.
My aim is to write a java programm in which I can get the information "which class is parentclass of a class?" and "to which class is a has_Relation connection?"
(I am not talking about individuals - I'm just talking about classes)
Thank you very much for your help in advance!
Best Regards
Natan
The simplest way to do this is to use an annotation property. In Protégé, select the class you want to relate to another class, then click the + beside "Annotations" in the Annotations tab. Then add the has_Relation property with the second button on the top left of the window. Then select the Entity IRI tab and the Classes subtab, select the other class you want to relate to and you're done.
However, you should rather not do this if has_Relation is an object property or a datatype property. If such is the case, you can use "punning", that is, you can make new individuals in the Individuals tab with the same names as the classes you want to relate. Then you relate them as if they were normal individuals. Note that this is allowed and valid in OWL 2 DL.
a bit late, but:
You can also go to the tabs menu and active the object properties tab
(Window> Tabs -> Object Properties )
Then you can create your own object property and assign its domain and range to which ever classes you want ( Description area of the individual property ).