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

佐手、 提交于 2019-12-07 12:58:01

问题


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?


回答1:


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:

  • ContextSwitcher
  • AjaxContent

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.




回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/565235/how-can-i-implement-jquery-in-my-zend-framework-application-in-a-custom-manner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!