在 Zend Framework 1.9 应用程序中开始使用 jQuery 的最佳方式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1616857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to start using jQuery in a Zend Framework 1.9 application?
提问by Andrew
I want to get started working with jQuery in my Zend Framework application but not sure which is the best way to get started. I know I could include the jQuery library just like any other javascript library, but what are the advantages of using ZendX_JQuery, and what are the steps necessary to start using it in my Zend Framework 1.9 application?
我想开始在我的 Zend Framework 应用程序中使用 jQuery,但不确定哪种是最好的开始方式。我知道我可以像任何其他 javascript 库一样包含 jQuery 库,但是使用 ZendX_JQuery 有什么优势,以及在我的 Zend Framework 1.9 应用程序中开始使用它的必要步骤是什么?
回答by Andrew
I was able to get jQuery working in my 1.9.4 project by following these steps:
通过执行以下步骤,我能够让 jQuery 在我的 1.9.4 项目中工作:
Step 1: Copy the ZendX directory to your librarydirectory. ZendX can be found in the extras/library directory of your Zend Framework download.
步骤 1:将 ZendX 目录复制到您的库目录。ZendX 可以在您的 Zend Framework 下载的 extras/library 目录中找到。
Step 2: Download jQuery and the jQuery UI library from jqueryui.com. I chose the UI Lightness theme.
第 2 步:从jqueryui.com下载 jQuery 和 jQuery UI 库。我选择了 UI Lightness 主题。
Step 3: Extract the download and rename jquery-ui-1.7.2 to jquery and move to your public/jsdirectory.
第 3 步:解压下载并将 jquery-ui-1.7.2 重命名为 jquery 并移动到您的public/js目录。
Step 4: Add these lines to your bootstrapfile:
第 4 步:将这些行添加到您的引导程序文件中:
protected function _initViewHelpers()
{
$view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
$view->jQuery()->addStylesheet('/js/jquery/css/ui-lightness/jquery-ui-1.7.2.custom.css')
->setLocalPath('/js/jquery/js/jquery-1.3.2.min.js')
->setUiLocalPath('/js/jquery/js/jquery-ui-1.7.2.custom.min.js');
}
Step 5: Now add the jQuery view helper to your layoutfile:
第 5 步:现在将 jQuery 视图助手添加到您的布局文件中:
<head>
<?php echo $this->jQuery(); ?>
</head>
Step 6: To test that you have everything working, add this line to one of your view scripts:
第 6 步:要测试您是否一切正常,请将此行添加到您的视图脚本之一:
Pick your Date: <?php echo $this->datePicker("dp1", '', array('defaultDate' => date('Y/m/d', time()))); ?>
Now, if you open this page in your browser, there should be a text field. You should be able to click on the text field, which automatically pops up a calendar that has been styled to the UI Lightness theme.
现在,如果您在浏览器中打开此页面,应该会有一个文本字段。您应该能够单击文本字段,它会自动弹出一个日历,该日历的样式已设置为 UI Lightness 主题。
回答by namespaceform
One little gotcha:
You have to add the ZendX folder to your librarydirectory - the one which also has your Zenddirectory.
一个小问题:
您必须将 ZendX 文件夹添加到您的库目录中——该目录也包含您的Zend目录。
[your/lib/path] | +-Zend | | | +-(the full thing) | +-ZendX | | | +-JQuery, Db, Console, ...
If you miss adding ZendX to your librarydirectory, you get lots of errors messages like this:
如果您错过将 ZendX 添加到您的库目录中,您会收到很多类似这样的错误消息:
Fatal error: Uncaught exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'JQuery' was not found in the registry; used paths: ZendX_JQuery_View_Helper_: ZendX/JQuery/View/Helper/ Zend_View_Helper_: Zend/View/Helper/: .....
Another little gotcha:
In the code presented by Andrew above, note the important words highlighted:
另一个小问题:
在上面 Andrew 提供的代码中,注意突出显示的重要词:
Now add the jQuery view helper to your layout file: <head> <? php echo $this->jQuery(); ?> </head> To test that you have everything working, add this line to one of your view scripts: <code> Pick your Date: <?php echo $this->datePicker("dp1", ..... </code>
While $this->jQuery()
must go in the layoutfile so that all pages get jquery functionality, the actual jQuery code must go to the view fileitself - application/views/scripts/yourcontroller/youraction.pthml
- it does not work in the layout with just this simple code.
虽然$this->jQuery()
必须进入布局文件,以便所有页面都获得 jquery 功能,但实际的 jQuery 代码必须进入视图文件本身application/views/scripts/yourcontroller/youraction.pthml
——它在仅使用这个简单代码的布局中不起作用。
回答by Ashish Sharma
The Solution is - >
解决办法是——>
protected function _initView()
{
$view = new Zend_View();
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$view->headTitle()->setSeparator(' - ');
$view->headTitle('IMR - BI System');
$view->env = APPLICATION_ENV;
$view->baseUrl = Zend_Registry::get('config')->root_path;
$view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
$view->jQuery()->addStylesheet($view->baseUrl . '/js/jquery/css/south-street/jquery-ui-1.8.2.custom.css');
$view->jQuery()->setLocalPath($view->baseUrl . '/js/jquery/js/jquery-1.4.2.min.js');
$view->jQuery()->setUiLocalPath($view->baseUrl .'/js/jquery/js/jquery-ui-1.8.2.custom.min.js');
$view->jQuery()->enable();
$view->jQuery()->uiEnable();
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
return $view;
}
I just shifted my Code from _initViewHelpers to _initView
我只是将我的代码从 _initViewHelpers 转移到 _initView
and it works for me.
它对我有用。
回答by EasyEcho
Just wanted to add that you have to (or at least I had to) enable the jquery and jquery components in the _initViewHelpers function:
只是想补充一点,您必须(或至少我必须)在 _initViewHelpers 函数中启用 jquery 和 jquery 组件:
$view->jQuery()->enable() ->uiEnable();
回答by Dr Casper Black
As user117640sad,
由于user117640伤心,
I had to enable the jQuery and UI, it can be done in:
我必须启用 jQuery 和 UI,它可以在:
bootstrap :
引导程序:
//it will enable for all views
$view->jQuery()->enable()->uiEnable();
controller::someAction :
控制器::someAction :
//JQ enabled for particular view)
$this->view->jQuery()->enable()->uiEnable();
view someAction.phtml:
查看 someAction.phtml:
//JQ enabled for particular view
<?php $this-jQuery()->enable()->uiEnable(); ?>
回答by Arvind
include this to ur bootstrap file
将此包含到您的引导程序文件中
$view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
$view->jQuery()->addStylesheet('/Your Public Path/js/jquery/css/ui-lightness/jquery-ui-1.7.2.custom.css')
->setLocalPath('/Your Public Path/js/jquery/js/jquery-1.3.2.min.js')
->setUiLocalPath('/Your Public Path/js/jquery/js/jquery-ui-1.7.2.custom.min.js');
Add this to ur layout
将此添加到您的布局
<head>
<?php echo $this->jQuery(); ?>
</head>
and use jQuery UI functions in ur view file: Pick your Date:
并在您的视图文件中使用 jQuery UI 函数:选择您的日期:
<?php echo $this->datePicker("dp1", '', array('defaultDate' => date('Y/m/d', time()))); ?>