本文介绍了什么是renderAjax(),它和render()有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用普通的 return this->render('create', ['model' => $model]) 时,我的弹出窗口完全失控了.当我更改为 return $this->renderAjax('create', ['model' => $model]); 时,一切都神奇地在正确的位置.我已经环顾四周以阅读有关 renderAjax() 的内容,但似乎绝对没有.有人能告诉我它有什么作用吗?我知道 ajax,但据我所知,它通常与 css 或 bootstrap 无关.

When I use normal return this->render('create', ['model' => $model]) my pop-up window goes all haywire. When I change to return $this->renderAjax('create', ['model' => $model]); everything is magically in their correct places. I have looked around quite a bit to read about renderAjax() but there seem to be absolutely nothing out there. Can someone tell me what it does? I know ajax but from what I know it usually has nothing to do with css or bootstrap.

推荐答案

要了解 render()renderAjax() 之间的区别,首先需要了解如何render() 有效.

To know the difference between render() and renderAjax() first you need to understand how render() works.

基本上,当 render() 被调用时,在视图中注册的每一段 JS 和 CSS 代码和文件引用都被收集在几个数组中,以便稍后在适当的位置呈现 - 这些位置存储在布局及其代码通过调用beginPage()head()beginBody()endBody() 呈现code> 和 endPage().

Basically when render() is called every piece of JS and CSS code and file references registered with the view is gathered in several arrays to be rendered later on in proper places - these places are stored in the layout and their code is rendered by calling beginPage(), head(), beginBody(), endBody() and endPage().

您可以通过在相关方法中设置第二个参数来指出 JS 代码应该呈现的位置 - 例如:

You can point where the JS code should be rendered by setting the second parameter in related methods - like for example:

$this->registerJs("alert()", yiiwebView::POS_HEAD);

渲染

<script type="text/javascript">alert()</script>

在方法 $this->head() 所在的布局中.

in layout where the method $this->head() is.

一切正常,直到您只想渲染视图的主要部分而没有布局.没有它(以及它的方法,如 beginPage()),JS 和 CSS 引用不能以以前的方式呈现,这就是为什么这个旋转正方形的花哨的 jQuery 代码不起作用 - JS 库还没有包括在那里.

Everything is working fine until you want to render only a main part of view without the layout. Without it (and its methods like beginPage()) the JS and CSS references cannot be rendered in the previous way and that is why this fancy jQuery code rotating the square does not work - JS library has not been included there.

当您调用 $this->render() 从视图中 或仅调用 $this->renderPartial() 从控制器来看,这正是正在发生的 - 未应用布局.

When you are calling $this->render() from within the view or just calling $this->renderPartial() from the controller exactly this is happening - layout is not applied.

renderAjax() 现在来救援了.

renderAjax() comes now to the rescue.

该方法不关心布局,因为它调用了beginPage()head()beginBody()endBody()endPage() 方法本身.多亏了这一点,每一段 JS 代码都可以附加到渲染的视图上,即使需要在 AJAX 弹出窗口中完成,jQuery 库也可以再次旋转这个正方形.

This method doesn't care about layout because it calls beginPage(), head(), beginBody(), endBody() and endPage() methods by itself. Thanks to this every JS piece of code can be attached to the rendered view and the jQuery library can rotate this square once again even when it needs to be done inside an AJAX popup.

这篇关于什么是renderAjax(),它和render()有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:03