[英]Best way to start using jQuery in a Zend Framework 1.9 application?
我想在我的Zend Framework應用程序中開始使用jQuery,但不確定哪種是最好的入門方式。 我知道我可以像任何其他javascript庫一樣包含jQuery庫,但是使用ZendX_JQuery有什么好處,在Zend Framework 1.9應用程序中開始使用它需要哪些步驟?
通過以下步驟,我能夠在我的1.9.4項目中運行jQuery:
步驟1:將ZendX目錄復制到庫目錄。 ZendX可以在Zend Framework下載的extras / library目錄中找到。
第2步:從jqueryui.com下載jQuery和jQuery UI庫。 我選擇了UI Lightness主題。
步驟3:解壓縮下載並將jquery-ui-1.7.2重命名為jquery並移至public / js目錄。
第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');
}
第5步:現在將jQuery視圖助手添加到布局文件中:
<head>
<?php echo $this->jQuery(); ?>
</head>
步驟6:要測試一切是否正常,請將此行添加到您的一個視圖腳本中 :
Pick your Date: <?php echo $this->datePicker("dp1", '', array('defaultDate' => date('Y/m/d', time()))); ?>
現在,如果您在瀏覽器中打開此頁面,則應該有一個文本字段。 您應該能夠單擊文本字段,該字段會自動彈出已設置為UI Lightness主題樣式的日歷。
一點點問題 :
您必須將ZendX文件夾添加到庫目錄 - 也是Zend目錄的目錄。
[your/lib/path] | +-Zend | | | +-(the full thing) | +-ZendX | | | +-JQuery, Db, Console, ...
如果您錯過將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/: .....
另一個小問題 :
在上面安德魯提供的代碼中,請注意突出顯示的重要單詞:
Now add the jQuery view helper to your file: <head> <? php echo $this->jQuery(); ?> </head> To test that you have everything working, add this line to one of your : <code> Pick your Date: <?php echo $this->datePicker("dp1", ..... </code>
雖然$this->jQuery()
必須放在布局文件中,以便所有頁面都能獲得jquery功能,但實際的jQuery代碼必須轉到視圖文件本身 - application/views/scripts/yourcontroller/youraction.pthml
- 它不起作用在布局中只有這個簡單的代碼 。
解決方案是 - >
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;
}
我只是將我的代碼從_initViewHelpers轉移到_initView
它對我有用。
只想添加你必須(或至少我必須)在_initViewHelpers函數中啟用jquery和jquery組件:
$view->jQuery()->enable() ->uiEnable();
由於用戶117640傷心,
我必須啟用jQuery和UI,它可以在以下位置完成:
bootstrap:
//it will enable for all views
$view->jQuery()->enable()->uiEnable();
controller :: someAction:
//JQ enabled for particular view)
$this->view->jQuery()->enable()->uiEnable();
查看someAction.phtml:
//JQ enabled for particular view
<?php $this-jQuery()->enable()->uiEnable(); ?>
將此包含在你的bootstrap文件中
$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');
將此添加到您的布局
<head>
<?php echo $this->jQuery(); ?>
</head>
並在你的視圖文件中使用jQuery UI函數:選擇你的日期:
<?php echo $this->datePicker("dp1", '', array('defaultDate' => date('Y/m/d', time()))); ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.