How to make an AJAX-based GUI using Zend Framework

I have an application built using the Zend Framework. I decided to go with jQuery instead of Dojo. I use ZendX helpers for things like ajaxLinks and dialogContainers. I want to try to make the GUI as possible AJAX. But I struggle to decide how I feel, this is the best approach. In most cases, it is a matter of loading the "page" in the dialogContainer instead of reloading the entire page. So that you, for example, get a dialog containing a form for changing some user data or something like that.

At first I returned the whole page, but if it was requested using an AJAX request, it used a different layout template to avoid all unenecerry javascript inclusions, etc. This allowed me to create one version of the page, which would basically be viewable, as usual And through AJAX. But I did not convince myself that I like it. Looking at the Zend Server GUI, they seem to do it, although they don’t return json-encoded data and build a page from it.

What would be the best approach for this and how should I handle javascript that is specific to the restored page? Now I have all the page specific javascript in the phtml file of this page.

Another thing I'm worried about is keeping track of resources created when opening a large number of dialog containers and filling them through ajax. Let's say we open one dialog and get a list of elements. If we click one more pop-up pop-up flag of the modal dialog box for this particular element and populate through ajax. But if the main page never reloads, I see that it becomes difficult to handle.

It seems like I can't be the first to want to make an ajax-driven user interface, so please point me in the right direction before I draw myself in the corner .;)

Update. I tried all kinds of tutorials that I could find on the topic of context, as well as on the documentation in Zend. I think I must have missed to include some kind of undocumented function or something like that. Since this did not work, I deleted the code, but tried to find the examples that I was looking at before I tried things like the following code.

$ajaxContext = $this->_helper->getHelper('AjaxContext'); $ajaxContext->addActionContext('list', 'html') ->addActionContext('modify', 'html') ->initContext(); 

And I tried something like the following

 $this->_helper->contextSwitch() ->setContext('html', array( 'suffix' => 'html', 'headers' => array('Content-Type' => 'text/html; Charset=UTF-8'), ) ) ->addActionContext('index', array('html','xml', 'json')) ->setAutoJsonSerialization(true) ->initContext(); 

I tried to add other ActionContexts etc., but no matter what they all just ended up displaying a regular .phtml file all the time.

+4
source share
1 answer

Here is what I learned when I made my ZF / Ajax application.

For HTML data:

You can use the ActiionContext action helper ( ZF Reference ). You can add an AJAX context to your actions. I use this in the init () function of the controller.

 $ajaxContext = $this->_helper->getHelper('AjaxContext'); $ajaxContext->addActionContext('myaction', 'html')->initContext('html'); 

What this means is that when myactionAction () is called, it checks to see if the request is being executed through an AJAX call. If so, it disables the layout and displays "myaction. Ajax .phtml" rather than "myaction.phtml" as if it were not an AJAX call. Thus, you can get pure HTML output from an action without any additional actions, you do not need to write request type verification code in each action. I found this to be generally easy work.

If the content you download contains additional javascript with you, be sure to add it to your view and repeat it.

 $this->headScript()->appendFile("/js/list.js"); echo $this->headScript(); 

Then JavaScript is executed, as usual.

For JSON data

JSON Action Helper is very fast and convenient in action for returning JSON data.

 $r = "Success"; $this->_helper->json($r); 

It also returns pure JSON and nothing more.

I think a more specific question will help you get better answers.

+1
source

Source: https://habr.com/ru/post/1338439/


All Articles