Yii renderPartial duplication solution

Its been almost a year since i found something interested to write since im busy working and didn't really get the time to write some useful stuff. Today i came across a well known issue in Yii solution that caused duplicate js request whenever we are doing ajax stuff with renderPartial. Here's a small js script that will save our asses.

        $.ajaxSetup({
                global: true,
                dataFilter: function(data,type){
                        //  only 'text' and 'html' dataType should be filtered
                        if (type && type != "html" && type != "text")
                        {
                                return data;
                        }
         
                        var selector = 'script[src]';
         
                        // get loaded scripts from DOM the first time we execute.
                        if (!$._loadedScripts) 
                        {
                                $._loadedScripts = {};
                                $._dataHolder = $(document.createElement('div'));
 
                                var loadedScripts = $(document).find(selector);
 
                                //fetching scripts from the DOM
                                for (var i = 0, len = loadedScripts.length; i < len; i++) 
                                {
                                        $._loadedScripts[loadedScripts[i].src] = 1;
                                }
                        }
                 
                        //$._dataHolder.html(data) does not work
                        $._dataHolder[0].innerHTML = data;
         
                        // iterate over new scripts and remove if source is already in DOM:
                        var incomingScripts = $($._dataHolder).find(selector);
                        for (var i = 0, len = incomingScripts.length; i < len; i++)
                        {
                                
                                if ($._loadedScripts[incomingScripts[i].src] && incomingScripts[i].src.indexOf('search') == -1)
                                {
                                        $(incomingScripts[i]).remove();
                                }
                                else
                                {
                                        $._loadedScripts[incomingScripts[i].src] = 1;
                                }
                        }
                        return $._dataHolder[0].innerHTML;
                }
        });

Credit goes to Yii forum contributor! but not all scenario will work, still, its good enough at least

Share
1&1 Hosting $.99* .COM Domain! Get going with GoDaddy!