views:

1011

answers:

4

How can I implement jquery in my Zend Framework application in a custom manner.

  • appending jquery.js ok
  • appending script ok
  • send POST data to controller ok
  • process POSTed data ok
  • send 'AjaxContext' respond to client now ok (thanks)

I'm using jquery for the first time, what am I doing wrong?

+7  A: 

Early on, the best practice to get Zend to respond to ajax requests without the full layout was to check a variable made available via request headers. According to the documentation many client side libraries including jQuery, Prototype, Yahoo UI, MockiKit all send the the right header for this to work.

if($this->_request->isXmlHttpRequest())
{
    //The request was made with via ajax
}

However, modern practice, and what you're likely looking for, is now to use one of two new helpers:

Which make the process considerably more elegant.

class CommentController extends Zend_Controller_Action
{
    public function init()
    {
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('view', 'html')
                    ->initContext();
    }

    public function viewAction()
    {
        // Pull a single comment to view.
        // When AjaxContext detected, uses the comment/view.ajax.phtml
        // view script.
    }

Please Note: This modern approach requires that you request a format in order for the context to be triggered. It's not made very obvious in the documentation and is somewhat confusing when you end up just getting strange results in the browser.

/url/path?format=html

Hopefully there's a workaround we can discover. Check out the full documentation for more details.

Matt Gardner
thanks for this hint, the context is indeed an important part of the whole story but now I got other problems...
tharkun
thanks for the code samples, I've done exactly that but as you can see from my edits, I have now another problem which is caused by using the AjaxContent
tharkun
maybe I'll just try your 'older' appraoch
tharkun
yeah.. I'm having a similar problem with ajax in jquery ui tabs. ajax context doesn't seem to be solving it.. but I haven't tried the older method yet.
Matt Gardner
Found it.. you have to request a format. Really takes out the elegance. Will see if there's a way to force the default.
Matt Gardner
Looks like there was a debate over this issue but it was never really resolved. http://framework.zend.com/issues/browse/ZF-3162
Matt Gardner
I simply add the following in my bootstrap (probably better to do it on an env variable):if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) }
Itay Moav
Maybe you can add a .htaccess rule that adds the ?format=html to it in case of an Ajax controller. The .htaccess should than be able to see if it's a ajax call, for install by prepending 'Ajax' to the name of the controller?
Roalt
A: 

This should have been a comment, can't, yet...
It has nothing to do with ZF+Jquery combination.
First try a proto of what you need with a simple php file. No framework, just Jquery and straight forward, dirty php.
Oh, and don't forget to track what happens with FireBug.

Itay Moav
my problem is definitely connected with ZF+Jquery
tharkun
+2  A: 

Make sure your using $(document).ready() for any jQuery events that touch the DOM. Also, check the javascript/parser error console. In Firefox it's located in Tools->Error Console. And if you don't already have it installed, I would highly recommend Firebug.

tj111
A: 

I have the same problem...