如何以自定义方式在Zend Framework应用程序中实现jquery? [英] How can I implement jquery in my Zend Framework application in a custom manner?

查看:81
本文介绍了如何以自定义方式在Zend Framework应用程序中实现jquery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以自定义方式在Zend Framework应用程序中实现jquery.

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

  • 附加jquery.js 确定
  • 附加脚本确定
  • 将POST数据发送到控制器确定
  • 处理发布的数据确定
  • 发送"AjaxContext"响应客户端现在可以(谢谢)
  • 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)

我是第一次使用jquery,我在做什么错了?

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

推荐答案

早期,让Zend在没有完整布局的情况下响应ajax请求的最佳实践是检查通过请求标头提供的变量.根据文档包括jQuery,Prototype,Yahoo UI,MockiKit在内的许多客户端库都发送正确的标头,以使其正常工作.

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.

这篇关于如何以自定义方式在Zend Framework应用程序中实现jquery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆