views:

750

answers:

3

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.

A: 

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

karim79
The problem I have with this, is that the function called action1Action() returns an error saying that "script 'athlete/action1.phtml' not found"
Roalt
you need to create a viewscript matching the name of your controller action, so if your controller is called AthleteController and your action is called testAction() you will need to create athlete/test.phtml
karim79
Creating a view script gets me a (recursive) view of my page layout inside that element. So it solves the error but does not give me the desired result. I think I should override some default Action behaviour.
Roalt
+1  A: 

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

Steve Goodman
Yes, I found this also out myself after looking around to find some solutions. The problem with the official documentation is that a lot of information is scattered around.
Roalt
Yeah, it can be a lot to sort through, but since the framework is so rich, I've found it worthwhile to set aside some time to read through the docs on a particular component I'm working with. I generally end up with a new perspective on how to use a particular component.
Steve Goodman
A: 

Hi Roalt,

i've got the same problem with the recursive view of my page, after clicking the ajax-link. did you found a solution meanwhile?

Regards NinSky

thats the solution in controller: $this->_helper->layout()->disableLayout(); $this->_helper->viewRenderer->setNoRender(); echo "Grüzi";
Yes, you should disable automatic rendering. Haven't managed to generate HTML in 'the zend way', but I acknowledge that you sometimes have to go around this HTML generation abstraction.
Roalt