zf2 dispatch event triggered after my own? - events

why dispatch event is triggered after my own in my controller?
onBootstrap:
$sharedManager->attach('*', '*', function($e) {
Debug::dump($e->getName());
});
indexAction:
$this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this);
Debug::dump('indexAction do...');
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this);
Result is:
string(5) "route"
string(15) "indexAction.pre"
string(17) "indexAction do..."
string(16) "indexAction.post"
string(8) "dispatch"
string(8) "dispatch"
string(6) "render"
string(8) "renderer"
string(13) "renderer.post"
string(8) "renderer"
string(13) "renderer.post"
string(8) "response"
string(6) "finish"
string(12) "sendResponse"
I found dispatch process:
https://docs.google.com/drawings/d/1OwFfjgaiXDuKmS2I8nnJNFqoDgEWNNB-_-tUXUPVrgQ/edit
but it seems that it's not correct
I thought that dispatch event will be triggered as dispatch.pre, not dispatch.post...

Your method of debugging is giving you strange results.
By adding the 'debug listener' with a default event priority (which is 1) the output of the event name actually happens after the dispatch event occurs.
This is due to the fact that ZF2 registers the controller's dispatch event first (also with an event priority of 1). So the event happens in the correct order; it's just your listener outputs at the wrong time.
You can test the order of events by adding one line to Zend\EventManager\EventManager::trigger()
// Zend\EventManager\EventManager
public function trigger($event, $target = null, $argv = array(), $callback = null)
{
echo '<br>Triggered: ' . $event;
//...
}
This output (excludes module bootstrap bits)
Triggered: bootstrap
Triggered: route
Triggered: dispatch // MVC dispatch
Triggered: dispatch // Action Controller disaptch
Triggered: foo.pre // Controller custom events
Triggered: foo.post // Controller custom events
Triggered: render
Triggered: renderer
Triggered: renderer.post
Triggered: renderer
Triggered: renderer.post
Triggered: response
Triggered: finish

Related

How can I display an Alert view from view model in Xamarin.forms

I working on Xamarin hybrid application, right now I am using MVVM architecture. My view model is 'INotifyPropertyChanged' interface class. I parse json data on this viewmodel class. Now I want to display alert if failed to read json data from server or need to show error message to user on Alert. But it not working. I know 'DisplayAlert()' will work only on Page Classes. So how can I show the alert from ViewModel Page. I tried this also, but not working;
await App.Current.MainPage.DisplayAlert(Constant.KSorry, Constant.KNoDataAvailable, Constant.KOK);
You can try putting the code inside Device.BeginInvokeOnMainThread
like,
Device.BeginInvokeOnMainThread(() =>
{
await App.Current.MainPage.DisplayAlert("Hello", "message", "OK");
});
Displaying an alert from main thread should work from ViewModel 'INotifyPropertyChanged' Interface class. Check the code
if (arrayAlbumList == null) {
Device.BeginInvokeOnMainThread(() => {
App.Current.MainPage.DisplayAlert(Constant.KSorry,
Constant.KNoDataAvailable, Constant.KOK);
});
}
else {
// binding the object here using array
}
On MVVM we can try the following
When you have just one button on the alert
DeviceDevice.BeginInvokeOnMainThread(() => { await App.Current.MainPage.DisplayAlert("Hello", "message", "OK"); });
When you have more than one button on the alert dialog
var response = await App.Current.MainPage.DisplayAlert("Hello", "message", "OK", Cancel");
No need for Device.BeginInvokeOnMainThread in this case

How to reenable formio submit button?

I'm using formio with angular to render a custom form I've built - https://help.form.io/developer/info/angular/
I'm using the following to catch the submission and call my own logic, however the submit button stays disabled. I'm not actually using the formio server side so how do I re-enable the submit button without changing the source code? Is there an undocumented event for form submission success? I've tried also binding to 'formSubmission' event.
$scope.$on('formSubmit', function(err, data) {
event.preventDefault();
submitForm();
}
For anyone else struggling with this you can call 'submitDone' but you need to $broadcast from your controller, not $emit ( $emit goes up the $scope chain, broadcast goes down )
$scope.$on('formSubmit', function(err, data) {
event.preventDefault();
submitForm();
}
function submitForm() {
var error = doStuff();
if ( error ) {
$scope.$broadcast('submitError', error);
} else {
$scope.$broadcast('submitDone', 'Success');
}
}
if you are using pure javascript, trigger the event with "emit" and intercept with "on", try this
formio.emit('submitError', res);// formio.emit('submitError', res);
The button with red background will be displayed by default
To intercept the event triggered by the emit, do the code below
formio.on('submitDone', function(res) { /*your code here*/ }

Ember RSVP promise stopped working

For some reason an RSVP promise I created in an action stopped working for me and I can't figure out what when wrong where to make it stop working.
I have a component that sends an action and waits for a response
// COMPONENT IN ITEM ROUTE
callAjaxAction() {
this.setProperties({working:true});
Ember.RSVP.cast(this.attrs.action()).finally(() => {
Ember.$('.draggable').animate({
left: 0
});
this.setProperties({working:false});
});
}
This particular instance of the component calls this action in the controller
// IN ITEM CONTROLLER
placeBid() {
return new Ember.RSVP.Promise((resolve,reject) => {
if(this.get('winning')) {
this.get('notification')._confirm({message: "You are currently the highest bidder, would you like to continue with your bid?",isConfirm: true}).then(()=>{
console.log('confirmed');
this.send('placeBidActual').then(()=>{
resolve();
}).catch(()=>{
reject();
});
return true;
}).catch(()=>{
resolve();
console.log('denied');
return false;
});
} else {
setTimeout(()=>{
this.send('placeBidActual').then(()=>{
resolve();
}).catch(()=>{
reject();
});
},500);
}
});
}
This action is calling a confirm method on a service and waiting for the user to hit yes in the case of the user already winning this item. Otherwise I'm just calling the actual ajax action right away, that action looks like this
// IN ITEM CONTROLLER
placeBidActual() {
return new Ember.RSVP.Promise((resolve,reject) => {
Ember.$.ajax({
...
}).then((response)=>{
(do some stuff with the response)
resolve();
}, (reason)=>{
(do something with rejection reason)
reject(reason);
});
});
}
In the console I'm getting the error
Uncaught TypeError: Cannot read property 'then' of undefined
On the line where it states this.send('placeBidActual')
UPDATE:
Here is maybe a better explanation to the expected process flow.
The user attempts to place a bid, the user swipes a component over to indicate they wish to bid. The UI at this point shows a loading indicator and waits for the ajax action to complete before it resets the UI.
If the user is not already the highest bidder of the item it will go straight to the ajax action and upon completion signifies to the component to reset the UI.
However, if the user is the highest bidder of the item it should instead show a confirm message (using the notification service I have setup) and wait for the user to either confirm or deny. If they deny it just cancels and signifies to the component to reset the UI. If the user confirms then it calls the ajax action and upon completion signifies to the component to reset the UI.
Updated answer:
This isn't working because send doesn't return the value of the action.
I suggest moving the placeBidActual action to a method on the controller and call it like any normal method. This way you will get the return value and be able to call .then on it.
You should pass a function, whithout invoke it.
Instead this:
Ember.RSVP.cast(this.attrs.action()).finally(() =>
Try it:
Ember.RSVP.cast(this.attrs.action).finally(() =>
Invoking the funcion this.attrs.action() does it pass undefined for the Promise.

Which events can be subscribed to with Knockout's observable.subscribe function

Under the Explicitly Subscribing to Observables section of the Knockout documentation, there is a reference to an event parameter of the subscribe function, but the only two examples given on that page are change and beforeChange.
By way of example, I tried passing in "focus" as the third parameter but that didn't work. I'm not too surprised as "focus" is an event of a DOM Element rather than a knockout observable, but nonetheless it could theoretically have setup a subscription to the focus event for all elements bound to that observable.
Is there a list of all events that can be subscribed to manually using Knockout's observable.subscribe function?
It make sens to use "event" binding in your case.
Because there are only two ways to notify subscribers of observable variable: beforeChange and change.
In knockoutJs code there is simple chain of if blocks which check if event is specified, and if event is equal to beforeChange. That's basically all logic which goes there, so no other events fired.
Part form knockoutJS which implements this logic:
self["notifySubscribers"] = function(value, event) {
if (!event || event === defaultEvent) {
self._rateLimitedChange(value);
} else if (event === beforeChange) {
self._rateLimitedBeforeChange(value);
} else {
self._origNotifySubscribers(value, event);
}
};

jqgrid info dialog function onClose

i am showing the server error message with an info_dialog.
I would like to fire a function, when the info_dialog ist getting closed. I have tried to do it with a mouse click, but it only fires after the dialog is already closed
first mouseclick: dialog closes, but alert is not fired
second and every following mouseclick: alert is fired.
I am using celledit.
Anyone with an idea how i can fire a function, when the dialog ist getting closed?
Thanks for your help.
errorCell: function(serverresponse, status) {
$.jgrid.info_dialog(
$.jgrid.errors.errcap,
serverresponse.responseText,
$.jgrid.edit.bClose,
{ zIndex: 1500}
);
$(document).click(function() {
alert( "Handler for .click() called." );
});
}
The method $.jgrid.info_dialog supports onClose callback which will be called on closing. The return value from the callback informs whether the closing is permitted. Just try the code
$.jgrid.info_dialog(
$.jgrid.errors.errcap,
serverresponse.responseText,
$.jgrid.edit.bClose,
{
zIndex: 1500,
onClose: function () {
alert("inside onClose");
return true; // allow closing
}
}
);
UPDATED: To catch closing of $.jgrid.info_dialog in case of clicking with the mouse outside of the dialog one have to do more complex trick.
var orgViewModal = $.jgrid.viewModal;
$.extend($.jgrid,{
viewModal: function (selector, options) {
if (options.onHide) {
options.orgOnHide = options.onHide;
options.onHide = function (h) {
alert("inside onHide");
return options.orgOnHide.call(this, h);
}
}
return orgViewModal.call (this, selector, options);
}
});
$.jgrid.info_dialog($.jgrid.errors.errcap, "Test message",$.jgrid.edit.bClose, {
zIndex: 1500,
onClose: function () {
alert("inside onClose");
return true; // allow closing
}
});
In the first part of the code I use "subclassing" of $.jgrid.viewModal method (like I used in the answer, this one and some other). So I forward all calls to original $.jgrid.viewModal method with one exception. If $.jgrid.viewModal method are called with onHide callback parameter I forward to original $.jgrid.viewModal method modified implementation of the callback. It allows to catch closing of the dialog.
UPDATED 2: The demo shows the approach live.
Alternatively (instead of subclassing) you can just modify the lines
onHide: function(h) {
h.w.hide().remove();
if(h.o) { h.o.remove(); }
},
of info_dialog in jquery.jqGrid.src.js. You need just insert additional call of mopt.onClose if the option defined. Probably one should include additional callback onClosed because onClose can deny closing, but new callback called inside of onHide can't do this.

Resources