get inner component from flexGlobals.topLevelApplication - actionscript-3

I feel the way inner components could be accessed be better than what I know.
I know we can access any inner component something like:
FlexGlobals.topLevelApplication.someChild1.someChild2.someChild3… and so on
I've a components which has number of parent components hierarchy. I wish to know if there could be anyway I could access the last child without referring all of its parent.
I need to trigger an event on that component.
FlexGlobals.topLevelApplication.child1.child2.child3.dispatchEvent(new Event('clearData', true));
Updated: I tried the way you suggested in point 1. I've added the event listener on child component and try to dispatch it from a action script file, but it went unheared.\
child3.addEventLisener('clearData', clearHandler);
And then I dispatched the event some thing like:
dispatchEvent(new Event(modelApp.CLEAR_PALETTE, true)

try to add
FlexGlobals.topLevelApplication.systemManger.addEventLisener('clearData', clearHandler);
in child3
and dispacthed it from any where you want,
for dispatching use the following
FlexGlobals.topLevelApplication.systemManager.dispatchEvent(new Event(modelApp.CLEAR_PALETTE, true)

How I understood from the question used no good principles of application design, but that's another question.
In your case, you can use stage as global currentTarget for bubbles events. Dispatch event from displayObject with bubbles = true; In target display object listen event with stage object with/out capture phase.
in any displayObject:
dispatchEvent(new Event("myEvent", true));
in target displayObject:
// add ADDED_TO_STAGE event listener in constructor or when component creation complete
protected function component_creationCompleteHandler(event:FlexEvent):void
{
addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
}
..
protected function onAddToStage(event:Event):void
{
stage.addEventListener("myEvent", eventHandler, true);
}
protected function eventHandler(event:Event):void
{
trace("eventHandler");
}
It is better than use reference to FlexGlobals object. Code more flexible.

I think you should go down to the level of component and addEventListener on initialize.
and dispatch from where ever you want.
other way is to do that, you need to save component reference(ID) in object on creationComplete. save its refence in your model. now you can access it where ever you want. and do what ever you want.
i also did in this way, i saved my screen components refrence in an array and access it where i need to dynamically validate all components.
otherwise you need nested looping to got your required child.

Related

How do you replace a listener in dart?

I have a class that creates a button and connects a listener to the click event. In a sub class I'd like to replace the superclass handler. This code adds a listener:
row.query("[value='Save']").onClick.listen(handleNewAlert);
How do I remove the existing listener?
The Stream.listen() method returns a StreamSubscription object. Call StreamSubscription.cancel() to cancel the event listener.
var subs = element.onClick.listen((e) => print(e));
// Remove the listener.
subs.cancel();
// Add another listener.
element.onClick.listen((e) => print(e));
See this article for more information.
Greg's answer describes how to unsubscribe from the event source.
This is correct, but IMO a bit awkward, as you need to keep the subscription instance around in case that any derived class wants to cancel it, or you need to provide a method for this on the base class if you want to hide the details.
As you say that you want to replace the handler, which I understand as providing a different implementation, a simpler approach is to simlpy override handleNewAlert method in the derived class.
Of course, it won't work if instead of method name you have specified an anonymous function, as it obviously can't be overridden, but in the scenario shown in your example, this might be an easier approach.

is there a dataProviderChange event for dropdownlist?

I have a case where the SelectedItem isn't called after the data provider changes for
a dropdownlist.
is there a way to detect if such a change occurred?
Thanks
Just about every public property in the Flex Framework has a 'propertyChanged' event that is dispatched from the properties set method. They are used primarily for binding purposes, but not usually formally documented.
The dataProvider set method of a DropDownList does indeed dispatch a dataProviderChanged event. The code is several layers up in the hierarchy, as part of SkinnableDataContainer or if the skin for your 'SkinnableDataContainer' has a dataGroup it is dispatched from the DataGroup.
But, this event will probably will not be exposed via code hinting in MXML. You'll have to listen for it in ActionScript:
myList.addEventListener('dataProviderChanged', onDataProviderChanged);
Keep in mind this event will always be dispatched when the dataProvider changes, regardless of what happens to the selectedItem property.
You can listen to changes in the selectedItem in a DropDownList through the IndexChangeEvent, illustrated by the code below:
<s:DropDownList id="dl_mydl"
dataProvider="{dataProvider}"
change="dropDownListChangeHandler(event)"
/>
protected function dropDownListChangeHandler(event:IndexChangeEvent) : void
{
//DO SOMETHING
}

How to access a displayobject on stage in a class that is not a document class

How to access a display object on the stage in a class which is not a document class?
I am looking for a solution that doesn't involve passing the stage as a parameter to the class.
One solution i always come across is using "TopLevel.as". Is it a good method because as far as I have heard using global variables is not recommended because it might cause some problems while working on big projects.
All display objects have a addedToStage event dispatched when they are added to the display list, which gives you a reference to the stage. So you could do something like this:
// In the constructor
addEventListener(Event.ADDED_TO_STAGE, onAdded);
// A class level function
private function onAdded(e:Event):void {
// get reference from stage, eg:
// stage.getChildByName("nameHere");
}
There is no need to use the "Top Level" class, which is relying on global (ie: static) properties.

Detect mouseUp on stage

Is there a way to check the method that has been attached to the stage?
I have stage as global.. and need to fire some function in a object on mouseup...
Now it fires 2 or 3 depending how many objects i add..
I need something like..
if($.stage.hasEventListener(MouseEvent.MOUSE_UP, this.mouseUp) === false){
$.stage.addEventListener(MouseEvent.MOUSE_UP, this.mouseUp);
}
Or a better way to handle this?
I'm guessing you are adding the listener inside each object, no? That means every time you create an instance of your object you are adding yet another listener for stage mouse up events. If you really only want a single listener for this type of event, move it outside of the scope of the object and only add the listener once. Good luck!

Dispatch Event When Listener Is Added - ActionScript3

Is it possible to dispatch an event from an object when an event listener is added to that same object, without overriding the addEventListener method of that class?
I am working on a modularized application in AS3. Several of the modules register events on a component in the main application mxml file. I would like to fire an event anytime an event is registered to the component from any module, without putting "dispatchEvent(someEvent)" after every addEventListener.
Any input would be greatly appreciated?
I don't know of any built-in that will help you, but you could just write your own function to encapsulate those.
public static function addEvent(ed:IEventDispatcher, evt:String, handler:Function) {
ed.addEventListener(evt, handler, false, 0, true);
ed.dispatchEvent(new Event("addedEvent"));
}