Firing Events With Yii2 - yii2

I'm trying to understand how I can apply event handlers to events in a Yii2 application. From reading the docs I believe I can fire off an event using this.
$this->trigger('failedTransaction');
However I am trying to understand where I put the handlers that are used for running when the failedTransaction event is fired.

You can use class-level events for this. You can attach this on your app bootstrap (and probably in config, before app is initialized):
Event::on(
MyClass::class,
MyClass::FAILED_TRANSACTION_EVENT,
[MyEventHandlerClass::class, 'handle']
);
Then whenever MyClass will call $this->trigger(self::FAILED_TRANSACTION_EVENT);, MyEventHandlerClass::handle() will be called to handle this event.
You can also use global events. In your config:
'on failedTransaction' => [MyEventHandlerClass::class, 'handle'],
And trigger event by Yii::$app->trigger('failedTransaction') - this will also call MyEventHandlerClass::handle() to handle this event.

Related

Actionscript: Is passing functions through events a good idea?

I have an event class which extends the normal events class so I can pass parameters and have custom events.
I have a message event which I use to display a message to the user, I send an event to trigger this message from many places in my project.
I now need to give the user an option in this message and access the answer in the original class which sent the event that triggered the message.
My solution is to pass a function as a parameter to the event listener, then on click the function in my original class is called.
Is this a good approach?
My other solution was to create more events to handle the various options, but I may end up with many types of events.
This is not a traditional delegation of concern. If the original class cares about an event, it should listen for it and react to it. If you wanted to extend "event" it would traditionally be to have it carry more data (or more descriptive data), not to have it carry a function closure.

libgdx: How to stop touchUp event?

As I can see from this article you can easily use "stop" method to stop event propagation, but this doesn't work for "touchUp/touchDown" event (event isn't stopped). I have also tested it for "mousemove" event and found that it works for it. Why? How can i stop "touchUp/touchDown" event?
PS: libgdx version: 1.2.0,
I'm using scene2d.
From github wiki:
The InputMultiplexer will hand any new events to the first InputProcessor that was added to it. If that processor returns false from the method invoked to handle the event, this indicates the event was not handled and the multiplexer will hand the event to the next processor in the chain. Through this mechanism, the MyUiInputProcessor can handle any events that fall inside one of its widgets and pass on any other events to the MyGameInputProcessor.
TL;DR: Return true from your handle function if you want to stop it from propagation.
Also, what version of LibGDX are you using? I use 1.2.0 and the touchUp() method returns a boolean, unline that in the tutaril from your question.

Firing a KeyDown event in WinRT

Is there a way to fire a custom KeyUp/KeyDown event on the CoreWindow?
For example, take the following event:http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.corewindow.keyup.aspx.
My application already uses CoreWindow::KeyUp and CoreWindow::KeyDown to handle events. I am trying to test that the correct delegates are being attached and thus called when an event happens.
Note that I can't call the delegate function directly since it will not test the fact that the delegate is attached to the event.
I am looking for an answer similar to https://stackoverflow.com/a/3977396/756356.
I doubt that's possible because it sounds like something possibly interfering with the sandbox concept of modern apps. You could maybe insert some layer between the CoreWindow and your handlers and bubble the events through that layer to make is possible to raise your proxy events. I would recommend against that though since that just adds code you don't need.

flex 4 - why dispatchEvent triggering creationComplete recursively

I have a custom component called shopView which is an MXML skinnable component. I have a controller class called ShopController which takes care of showing this component in popup, updating info shown in the component etc.
Now, I wanted to maniupate some of the subcomponents of this ShopView after it has been created from the controller after the ShopView is created (creationComplete() event)
So, I have attached an event listener which intern does some initialization process
creationComplete="init(event)"
the init() function
private function init(event:FlexEvent):void{
event.stopImmediatePropagation();
initMenus();
initSlots();
dispatchEvent(event);
}
Attached another creation complete event from the controller class
_shop.addEventListener(FlexEvent.CREATION_COMPLETE,onShopCreated);
*_shop is the instance of ShopView*
Now, if you see the init() function, there I am stopping the event propagation, doing some initialization process and after that I am dispatching the event (for the shop controller do the rest of the job)
Now, this is crashing the app because the crationComplete event of the ShopView is recursively called. I was thinking the dispatchEvent will propagate to the other listerners but seems like it is propagating back to the same component.
I have fixed it by removing the e.stopImmediatePropagation() and dispatchEvent(event) lines from the init() function. But I want to know why it is happening like this?
Is it a known issue for the mxml/flex components? OR it is expected behavior?
Update: I am not doing same in .as as I said below. Got answer, basically its my stupidity :)
because I have not seen this behavior when I write .as classes where I
stopevent propagation and dispatch the event based on business logic.
Thanks in advance.
This is expected behavior.
When you redispatch an existing event dispatchEvent automatically clones it (since you can't dispatch the same event twice.) This clears any propagation-related flags.
May I ask why you want to redispatch CREATION_COMPLETE in this situation anyway? Both handlers will function just fine without the two lines you removed.

Views and Controllers invisible to each other? How can this be?

I have read that Views and Controllers should be invisible to each other.
How does the controller then listen for say a button click in the UI view?
Surely the controller needs to know about the view to addEventListeners or the UI view needs to call functions in the controller, or dispatch events to the controller.
My only experience of MVC is with using the robotlegs framework so I'll try explain my approach when using this framework
My views only use native flash events (MouseEvent.CLICK etc) which are listened for in the mediator for the view. When the mediator receives a native event it will then dispatch an application specific event, LoginEvent.LOGIN for example, which can be mapped to then fire a command using the command map on your context.
Sorry if that doesn't make much sense, but basically your views fire native events and your mediators will listen for these native events and then fire application specific events which in turn run your commands. You use the mediator as the 'ears' for your view and keep any logic out of them. The application context allows you to fire commands whenever certain events occur so your views and commands are seperated.