什么是renderAjax(),它与render()有何不同? [英] What is renderAjax(), how is it different from render()?

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

问题描述

当我使用普通的return this->render('create', ['model' => $model])时,我的弹出窗口将一团糟.当我更改为return $this->renderAjax('create', ['model' => $model]);时,一切都神奇地放在了正确的位置.我到处逛逛了很多,以了解有关renderAjax()的信息,但似乎绝对没有.有人可以告诉我它做什么吗?我知道ajax,但据我所知,它通常与CSS或引导程序无关.

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()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()", \yii\web\View::POS_HEAD);

渲染

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

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

一切正常,直到您只想渲染视图的主要部分而没有布局.没有它(及其方法,如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()有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆