Tutorial :How does the backend look for AjaxLink() call in Zend Framework



Question:

I'm trying to make some ajax-functionality in my web application, but I cannot get all puzzle pieces to fit:

I want to add a link that, when clicked upon, will open a new input (text) field that can be filled by the user. In the back-end, I want to do some administration that the link is clicked.

I want to do according to the Zend Framework principles, with using the ajaxLink() method. Can anyone have an example for me? I've read the official documentation (ZendX_JQuery) but it doesn't fully help me.

My front-end (view) code looks like this;

<?= $this->ajaxLink("Subscribe", $this->url(array('controller' => 'mycontroller', 'action' => 'action1', 'id' => $event['id'])),                                          array("beforeSend" => "hide",                                                 "update" => "#pb_" . $event['id'],                                                'noscript' => false,                                                 'method' => 'POST')); ?>    

My back-end code looks like this.

public function action1Action()      {          if( !$this->loggedIn || ! $this->athlete) {              $this->_redirect('index');          }          if(! $this->_request->isXmlHttpRequest())          {              //The request was NOT made with JS XmlHttpRequest              die;          }          // Do some administration          // (removed to make this easier in this example)            $pb = new Zend_Form_Element_Text('PB');          $pb->setLabel('PB:')                    ->addValidator('StringLength', false, array(0,20))                    ->setRequired(false);          $renderText = $pb->render();            return $renderText;        }  

I keep getting errors back that the given method wants to look-up a action1.phtml view script. I'm also not sure if what I try to do with the generation of the form input element works in this way.

I found some of the answer in this question, but it's not that elegant (requires an extra parameter in the link and you need another controller) which I don't like.


Solution:1

You need to turn off the ViewRenderer for this particular action. ZF by default enables an Action Helper called ViewRenderer which assigns a conventionally named view script (in your case, action1.phtml) to a particular action method. Since you're only trying to return a small snippet of text, rather than a full site view, full view rendering isn't necessary. Fortunately, this is easy.

 public function action1Action(){       $this->_helper->viewRenderer->setNoRender();       // the rest of your code   }  

The full docs are here: http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.viewrenderer


Solution:2

There's nothing to it. All you have to do is point it to where your content is coming from. In your view:

   <?= $this->ajaxLink("Example 1","/controller/action1",                                       array('update' => '#content',                                             'noscript' => false,                                             'method' => 'POST')); ?>  

In your controller:

echo 'Some Content';  

Read this:

http://www.mikaelkael.fr/IMG/pdf/ZendX_Framework_1.7.x_EN.pdf


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »