MessageHandler captured in various tabs - Flex - actionscript-3

The architecture is based on the use of the following frameworks: Flex, Cairngorm and Parsley.
I use a dispatcher with an event "AdicionarItemVendaMercadoriaEvent" within a popuppanel: PopupPanel.
I capture the event with [MessageHandler] within the FormPM.as injected into Form.mxml.
Within Form.mxml, I have a mx: TabNavigator and each tab is within one s: NavigatorContent.
It turns out that when there is more than one open tab mx: TabNavigator the dispatched event is captured in all Form of all tabs.
Normal operation was to capture the only event of the tab where the PopupPanel was dispatched, not all tabs.
Please, any suggestions for solving this problem?
I appreciate the help.

How about using straight forward function callbacks?
When you create your PopUpPanel, pass in a function callback that you want to be executed when the popup is closed. Rather than using an event, you'd simply call the function. i.e.
Inside FormPM:
public function showPopup():void
{
var popup:PopUpPanel = new PopUpPanel();
popup.onCompletion = handleResult;
PopUpManager.addPopUp(popup, ...);
}
private function handleResult(someData:*):void
{
// My popup has completed.. what do I want to do with the result.
}
You might want to consider the Spicelib 3.0 command framework and have a command to popup your panel and then add the error/success callbacks to that: http://www.spicefactory.org/parsley/docs/3.0/manual/?page=commands&section=intro

Related

Is there a "broadcast" method in HTML/CSS/Javascript?

So in the site scratch.mit.edu, there is a way so that once a block of code is finished, it can broadcast a statement. Its a really useful tool and I was wondering if there is something like/similar to that in coding!
Does anyone know if there is or not?
Look into creating and triggering custom events.
Example:
//create a new custom Event object called build
const event = new Event('build');
// create an event listener (attached to the global window object)
//This is like a Scratch receiver hat block. The first parameter, 'build', is the
//name of the broadcast. The second parameter, the function, is your code
//that reacts to the broadcast.
window.addEventListener('build', function (e) { /* your code here */ });
//This line of code "sends" the broadcast.
window.dispatchEvent(event);
Have fun learning JavaScript!

Flex event listener for Alert show

Is it possible to add an event listener for Alert.show?
So that every time an alert box is shown I can call a function to hide an iframe.
I think you will get stuck since event dispatching/listening will require using an instance of a class to be the dispatcher, which is not the case when using the static .show().
However, I guess you could manually dispatch an event every time you want to close your iframe and display an alert (both could be done by the event dispatched).
You could as well create your own class that would have a .showAlert function performing both the event dispatching and the regular Alert.show(). This requires your custom class to be instantiated but the instance could also be stored in a Singleton so you don't have to recreate a new one every time you want to display your Alert.
Alert.show will return you the instance of the alert object. Use the object to add event listeners on the alert.
var alert:Alert = Alert.show("contente");
alert.addEventListener(Event.Close, function(e:Event):void{
// TODO
);

Swing JTabbedPane : addChangeListener or addContainerListener or both?

I have one swing code written by other person. For swing tabbed pane, he has added both change and container listener and both calls the same method:
addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent theEvent ) {
someMethod();
}
} );
addContainerListener(new ContainerAdapter() {
public void componentAdded(ContainerEvent theEvent) {
someMethod();
}
public void componentRemoved(ContainerEvent theEvent) {
someMethod();
}
} );
Whenever tab is removed from this tabbed pane, it internally calls JTabbedPane.removeTabAt(int index), which in turn calls fireStateChanged() causing new change event listened by change listener.
Now as new component (tab) is removed from tabbed pane, it also calls componentRemoved(ContainerEvent theEvent) method of container listener.
Both change even and container events, then calls same method someMethod(), which does set background and foreground colors.
I would like to know, if this kind code might cause some issues. Recently we are facing random IndexOutOfBoundException exeptions. I am just wondering, if this is causing this issue.
Also as per my understanding in swing, once event is listened, logic inside it should be executed using worker thread (e.g. SwingWorker). Please let me know if this is correct.
I am new to swing, thus any hint would be appreciated.
Thanks.
Whenever tab is removed from this tabbed pane, it internally calls
JTabbedPane.removeTabAt(int index), which in turn calls
fireStateChanged() causing new change event listened by change
listener.
This is true if the removed tab is also the selected tab. In the other cases, you won't be notified.
You need to choose what event you want to listen to:
Addition/Removal of components?--> go for ContainerListener
Selected tab? --> go for ChangeListener
I would like to know, if this kind code might cause some issues.
Recently we are facing random IndexOutOfBoundException exeptions. I am
just wondering, if this is causing this issue.
Since there is no line in your sample code that could throw that Exception, it is impossible to answer your question. Post an SSCCE that shows your issue.
Also as per my understanding in swing, once event is listened, logic
inside it should be executed using worker thread (e.g. SwingWorker).
Please let me know if this is correct.
It depends:
If you need to modify anything in the UI, anything related to Swing, it needs to be executed on the EDT (Event Dispatching Thread) and thus, SwingWorker is not an option.
If you need to perform business logic operations, and especially if they can be lengthy, then you should indeed use a SwingWorker or any other mechanism to execute that code in another thread than the EDT. Consider visiting the Swing tag wiki on "Concurrency"

Actionscript 3 - Is it ok to have a bunch of code inside an event listener handler?

I'm creating a GUI, but only if things go ok, so can i
addEventListener(Event.Complete, go) to something and in the go function create my GUI (grafical elements such as labels, lists, squares)?
Is it ok to do that?
Technically it's fine. crooksy88 gives a good example of supplying a default value for the event parameter to make the function more versatile.
However, for the sake of semantics, clarity, and maintenance I would usually prefer to separate things more. So mine might be set up more like this:
protected function onLoadComplete(e:Event):void {
initAppSettings();
createUI();
startApp();
}
It makes it much easier to understand the flow of the app and what each part does just by reading the function names. When I come back to this later, I'll know that my UI is created in the function named createUI and not have to figure out that it gets created in an event handler with a cryptic name like go or handleEvent.
Also, if I want to change the flow of my app, say to pop up a dialog once the load is complete before the UI is created, I just have to move around some function calls, instead of moving around large chunks of code.
Yes that is perfectly fine. The go function isn't part of the event listener.
function go(e:Event):void {
// do something
}
The sample above requires the event parameter from the listener (e:Event).
But you can modify the function so that the parameter is optional so you can call the go function any time you want
function go(e:Event = null):void {
// do something
}
The example above will be triggered by the listener and also by typing
go();

How to hook a keyboard event handler onto an INPUT element with jQuery?

I want to attach my own key event handler to an INPUT that already has another event handler attached to onkeydown. Essentially, I want to receive a key event before all the other handlers and check if the user pressed a certain key -- if yes, I want to perform some functions and discard the event, if no, I want to pass it along to the other handler(s).
How can I do this with jQuery?
If you are loading a 3rd party script or jQuery addon you can just load your script or function first. If you do that then you can use something like this without the mess of unbinding and rebinding event handlers.
// your possible interceptor code
$("#awesome").keydown(function(e) {
if (e.keyCode < 70) {
e.stopImmediatePropagation();
console.log("BLOCKED!!!");
};
});
// possible 3rd party event code loaded after your code
$("#awesome").keydown(function(e) {
console.log("3rd party:"+e.keyCode);
});
Example webpage => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-key-event-interception.html
Example output of Firebug console
jQuery stopImmediatePropagation() documentation
According to the jQuery bind() documentation:
"When an event reaches an element, all handlers bound to that event type for the element are fired. If there are multiple handlers registered, they will always execute in the order in which they were bound."
So it looks like you will have to unbind the other handlers, bind yours and then add the others back if you want yours to run first.
There is some good information in this thread with respect to that:
jQuery: Unbind event handlers to bind them again later