Javascript Zend headScript() 和 appendFile 没有按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2434506/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:18:34  来源:igfitidea点击:

Zend headScript() and appendFile not working as expected

javascriptzend-frameworkappendfile

提问by Ryan

I'm having an issue trying to append a javascript file using headScript()->appendFile('file name')with Zend. I have my layout setup like this:

我在尝试使用headScript()->appendFile('file name')Zend附加 javascript 文件时遇到问题。我的布局设置是这样的:

    <?= $this->headScript()
    ->prependScript( 'BASE_URL = "' . $this->baseUrl() . '";' )
    ->appendFile( $this->baseUrl('js/lib/jquery/jquery-1.4.2.min.js') )
    ->appendFile( $this->baseUrl('js/admin.js') );

?>

Then, in my controller I am trying to append an additional js file only for this page, like:

然后,在我的控制器中,我试图仅为此页面附加一个额外的 js 文件,例如:

    $this->view->headScript()->appendFile( 'another/js/file.js' );

This file needs to be appended to what is already setup in the layout. However, this file gets added beforethe other 'appendFile' files. I've also tried

此文件需要附加到布局中已设置的内容中。但是,此文件会其他“appendFile”文件之前添加。我也试过

$this->headScript()->offsetSetFile(999, '/js/myfuncs.js');

But this still adds the file before the other files. This is not how I would expect this to work, especially when using the offsetSetFile method. How can I add this file afterthe other files? Thanks.

但这仍然会在其他文件之前添加该文件。这不是我期望的工作方式,尤其是在使用 offsetSetFile 方法时。如何在其他文件之后添加此 文件?谢谢。

回答by ejnadall

The answer is to use headScript()->prependFilein layout.phtml and headScript()->appendFilein a view.

答案是headScript()->prependFile在 layout.phtml 和headScript()->appendFile视图中使用。

回答by jtymann

I know it's a late answer but!

我知道这是一个迟到的答案但是!

If you do appendFile or appendScript is uses the next available index. Thus

如果您执行 appendFile 或 appendScript,则使用下一个可用索引。因此

$this->headScript()->offsetSetFile(50, 'file');
$this->headScript()->appendFile('file2');

is equivalent to

相当于

$this->headScript()->offsetSetFile(50, 'file');
$this->headScript()->offsetSetFile(51, 'file2');

Also it is key to note that the controller code get executed first before the view/layout code. Thus in your case your appends are actually using the id's 1000, and 1001. A quick fix to this is just to explicitly use offsetSetFile for all your appends. So your code in your layout would look like:

另外需要注意的是,控制器代码在视图/布局代码之前首先执行。因此,在您的情况下,您的追加实际上使用了 id 的 1000 和 1001。对此的快速解决方法是明确地将 offsetSetFile 用于所有追加。因此,您布局中的代码如下所示:

<?= 
   $this->headScript()
      ->prependScript( 'BASE_URL = "' . $this->baseUrl() . '";' )
      ->offsetSetFile(500, $this->baseUrl('js/lib/jquery/jquery-1.4.2.min.js') )
      ->offsetSetFile(501, $this->baseUrl('js/admin.js') );
?>

I hope this helps future googler's

我希望这有助于未来的 googler

回答by Ryan

I've noticed that if I 'prepend' all the files in the layout, I can then use appendFile in my controller and it will appear after them. Unfortunately, then I have to list all my JS files in reverse order in the layout (since they prepend to themselves). I'd really like to keep things in order and just be able to append to my layout stack.

我注意到如果我在布局中“前置”所有文件,然后我可以在我的控制器中使用 appendFile 并且它会出现在它们之后。不幸的是,然后我必须在布局中以相反的顺序列出我所有的 JS 文件(因为它们在自己的前面)。我真的很想让事情井井有条,并且能够附加到我的布局堆栈中。

回答by smack0007

If prepending in the layout file is not good enough, you can either include the files in your bootstrap when you set up your view or you could set up a controller plugin if you need to prepend based on what module is being loaded.

如果布局文件中的前置不够好,您可以在设置视图时将文件包含在引导程序中,或者如果您需要根据正在加载的模块进行前置,则可以设置控制器插件。

// in your bootstrap
protected function _initHeadScript()
{
    $this->bootstrap('view');
    $view = $this->getResource('view');

    $view->headScript()->appendFile('another/js/file.js');
}

// as a plugin
class My_Plugin_HeadScriptPlugin extends Zend_Controller_Plugin_Abstract
{
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        $view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;

        if($request->getModuleName() == 'foo')
        {
            $view->headScript()->appendFile('another/js/file.js');
        }
    }
}

回答by Rodrigo Souza

Actually, you don't need get the baseUrl 'cause ZF already does it for you. You just have to pay attention to your path. Do not use the first slash! otherwise ZF will return the remote address.

实际上,您不需要获取 baseUrl,因为 ZF 已经为您完成了。你只需要注意你的路径。不要使用第一个斜线!否则 ZF 将返回远程地址。

Just use '$this->_view->headLink()->appendStylesheet('css/main.css');'

只需使用'$this->_view->headLink()->appendStylesheet('css/main.css');'

回答by max4ever

http://zendframework.com/issues/browse/ZF-3282?focusedCommentId=23552&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-23552

http://zendframework.com/issues/browse/ZF-3282?focusedCommentId=23552&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-23552

the last comment gives an excellent solution, first include in the layout at top, then print when you need it, this way it will work

最后一条评论给出了一个很好的解决方案,首先包含在顶部的布局中,然后在需要时打印,这样它就会起作用