Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger Zend FlashMessenger in jQuery

I'm facing an issue and I was wondering if someone could point me in the right direction.

I am using Zend's FlashMessenger in my controllers, but in one of my files I handle my forms via AJAX posts. I thought Humane-flatty would be a nice replacement for Zend FlashMessenger in that case, but the layout of it is completely different.

Is there any way to trigger Zend Flashmessenger in my AJAX post?

This is how I currently call Zend FlashMessenger in my controllers:

$this->_helper->FlashMessenger(array('success' => 'Succesfully added'));

This is my jQuery:

if (data.weight !== '' && data.customer != '') {
   $.ajax({
     type: 'POST',
     url: '/index/line',
     data: data,
     success: function(res) {
       $('#open_orders').append(res);
       flashSucces.log('Orderline succesfully added.');
       //Can I call Zend flashmessenger from here?
     }
   });
}
like image 985
Matheno Avatar asked Apr 29 '15 09:04

Matheno


Video Answer


1 Answers

I suppose your URL, function line in your index controller "index/line", returns some JSON/XML or some HTML which you return and append into a div with #open_orders id.

The idea is, your ajax call needs to return a function from your controller which calls your FlashMessenger call,

$this->_helper->FlashMessenger(array('success' => 'Succesfully added'));

In other words that code needs to run from your controller, and returned to your client (callback of your ajax call in your jquery statemtent) via a direct call.

You may either return partial responses from your "res" and split them and append the correct part into your div, and the other part you append to a preceding div with a different id, so that your injected FlashMessenger code works,

OR

Where you have the following comment

//Can I call Zend flashmessenger from here?

you call another ajax call to another function in your controller where you call the FlashMessenger code in that function, so that you call both your required bits and the FlashMessenger

TLDR: You need that FlashMessenger call in your Zend(Server) side, and call that function in your controller from your client via the .ajax call

Hope this helps.

like image 125
sed Avatar answered Sep 22 '22 04:09

sed