简体   繁体   English

将自定义jquery和javascript放在Zend Framework中的最佳位置在哪里?

[英]Where is the best place to put the custom jquery and javascript in Zend Framework?

where is the best place to put the jquery and javascript scripts in zend framework? 将jquery和javascript脚本放入zend框架的最佳位置在哪里?

shall i put every script in js file and append in the controller like : 我应该把每个脚本放在js文件中并追加到控制器中,例如:

$this->view->headScript()->appendScript $ this-> view-> headScript()-> appendScript

or to leave in the phtml page as is ? 还是按原样留在phtml页面中?

I'd say it depends on your scripts, if your script is linked to your controller, you could include it in the _init() method of your controller. 我想说这取决于您的脚本,如果您的脚本链接到控制器,则可以将其包含在控制器的_init()方法中。

I usually prefer to include my .js scripts inside my view (when they concern only one unique view) using $this->jQuery()->addOnload() for jQuery and: 我通常喜欢将$this->jQuery()->addOnload()用于jQuery并将.js脚本包含在视图中(当它们仅涉及一个唯一视图时$this->jQuery()->addOnload() ,并且:

<?php $this->headScript()->appendFile('/js/user-list.js') ?>
<?php $this->headScript()->captureStart() ?>
site = {
    baseUrl: "<?php echo $this->baseUrl() ?>"
};
<?php $this->headScript()->captureEnd() ?>

for js scripts. 用于js脚本。 As you can see in this second example, a real advantage to use captureStart() is that you can use PHP to generate contents in your Javascript . 在第二个示例中可以看到, 使用captureStart()真正优势在于可以使用PHP在Javascript中生成内容 It can be pretty useful for translating a word for example. 例如,翻译一个单词可能非常有用。

Finally, _initView() method in your bootstrap is a good place to put web-site relative .js. 最后,引导程序中的_initView()方法是放置网站相对.js的好地方。

  • Javascript you need for the entire site you want to add during bootstrap , in a view setup or initView method. 在视图设置或initView方法中,需要在bootstrap期间添加整个站点的 Javascript。
  • Javascript you need for some controller , you should append in the init() method of that controller. 某些控制器所需的Javascript,应在该控制器init()方法中追加。
  • Javascript you only need for a certain action , you should append in that action. 您只需要执行某个动作的 Javascript,就应该在该动作中追加。

This way the client never has to download more data/files than really needed. 这样,客户端永远不必下载比实际需要更多的数据/文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM