3

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?

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks for this hint, the context is indeed an important part of the whole story but now I got other problems...
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
maybe I'll just try your 'older' appraoch
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.
Found it.. you have to request a format. Really takes out the elegance. Will see if there's a way to force the default.
|
2

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.

Comments

0

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.

1 Comment

my problem is definitely connected with ZF+Jquery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.