开发者

How to specify JS script to be included after ajax call in CListView

开发者 https://www.devze.com 2023-03-24 16:05 出处:网络
After an ajax call I am calling a function $this->widget(\'zii.widgets.CListView\', array( \'viewData\'=>array(\'sent\'=>$sent),

After an ajax call I am calling a function

$this->widget('zii.widgets.CListView', array(
            'viewData'=>array('sent'=>$sent),
            'dataProvider'=>$dp,
            'pager' => array(
                            'prevPageLabel'=>'< Anterior',
                            'nextPageLabel'=>'Siguiente >',
                            'header'=>'Pagina: ',
                            'pageSize'=>5,),
            'template'=>"{items}\n{pager}", //template
            'itemView'=>'_messageView',
            'emptyText'=>'No ha recibido ningun mensaje todavia',
            'enablePagination'=>true,
            'baseScriptUrl'=>'/../../Javascript/messages/messages.js',
            'afterAjaxUpdate' => 'js:callFunction()',
            'id'=>'listMessages',
        ));

The function callFunction() is defined in a separate js file which i include the first time the pag开发者_Go百科e loads, like this:

<script type="text/javascript" src="/../../Javascript/messages/messages.js"></script>

The function gets called the first time i load the page, but after an ajax call it doesnt get called anymore. I suspected it was because it couldn't find the message.js file, so i tried including it again adding this piece of code to the CListView definition: 'baseScriptUrl'=>'/../../Javascript/messages/messages.js', but now im getting this error: jQuery("#listMessages").yiiListView is not a function ... Any ideas?

Solution

                    $this->widget('zii.widgets.CListView', array(
                    'dataProvider'=>$ligas,
                    'viewData'=>array('joinedArray'=>$joinedArray),
                    'template'=>"{items}", //template
                    'itemView'=>'_viewLiga',
                    'emptyText'=>'Usted todavia no se ha unido a ninguna liga',
                    'afterAjaxUpdate'=>'js:function(id, data) {myLigasListUpdated();}',
                    'enablePagination'=>false,
                    'id'=>'listViewMyLigas',
                ));

The problem was in the signature of the function which had to be called after an ajax update. It had to be like stated in the solution. Thanks @jfriend00


There is never a need to reload a script file in order to use functions in it unless you went to a new page or went to a different scope that can't see the functions or unless something destroyed the script tag. If you're still on the same page and still in an appropriate scope, then the functions are still there. You should concentrate on why the functions aren't working rather than trying to load a script file a second time.

If you can show us the actual generated javascript that contains the ajax call, we can help you figure out why it doesn't work the second time around. Much, much better to fix the root cause rather than pursue a hack work-around of reloading.

0

精彩评论

暂无评论...
验证码 换一张
取 消