Cclientscript::pos_Ready Vs. Renderpartial

Hi,

What’s the reason for when we are using renderPartial (typically in ajax actions) then we cannot add scripts with cclientscript to the document ready event?

The relevant code in CClientScript:




public function renderBodyEnd(&$output)

{

...

                if(isset($this->scripts[self::POS_READY]))

		{

			if($fullPage)

				$scripts[]="jQuery(function($) {\n".implode("\n",$this->scripts[self::POS_READY])."\n});";

			else

				$scripts[]=implode("\n",$this->scripts[self::POS_READY]);

		}

...

}



Where $fullPage will evaluate as true, when the output contains the ‘body’ tag.

I don’t know, if I understand correctly, what you’re asking about, because your question is asked a little bit strange. But I have the feeling, that you’re mixing two separate things.

renderPartial is used to render part of a bigger view and generally has nothing to do with AJAX. You may use it (though anything that is returned as AJAX reply should be as simple, as possible, so using renderPartial here is not that wise idea) or you may not use it at all.

Second thing is, why you can’t register Javascript code in AJAX. Well… if I’m not mistaken, you can, but it won’t work. As I recall from my memory (correct me, if I’m wrong), any Javascript code that comes up via AJAX reply won’t be executed by a browser.

I remember in an old project, that I was sending back via AJAX a part of a view, very simple, but yet folded / hidden in a div and with a link to unfold / unhide it. I added jQuery .click event for that link and inside I wanted to use simple .toggle function to unfold / unhide hidden part. That failed, and after doing some research I found above clue – no Javascript / jQuery code will be executed by a browser, if it’s source code comes via AJAX.

(this is at least three years old knowledge, so feel free to correct me, if I said some stupid things)

Maybe my question wasn’t clear, so let me explain it…

At first, the facts:

Browsers don’t parse scripts tags inserted into the DOM, but jquery does. The second fact is that you can register scripts at any time.

So lets say you are developing an app which uses ajax to load pages. In this case the simpliest solution is send back the view rendered by renderPartial,and insert the response into the DOM with jquery on client side. If you rendered the view with processoutput=true, your registered scripts will be added to the output. But as you see from the code above, if the output is not a full page, your registered scripts at pos_ready won’t be wrapped in by the document.ready function.

So my question is that why will my scripts be rendered into the document ready function ONLY if the output is a full page?

I hope that it’s clear now…