Title: afterAjaxUpdate callbackfunction CListView shows undefined
Tags: jquery;ajax;yii;yii-components
Question: i am trying to call a function which is defined in another js file by afterAjaxUpdate paramter of the file name but i got error in console that function is not defined
<?php
$dataProvider=new CActiveDataProvider('profiles',array('pagination'=&gt;array('pageSize'=&gt;3))); ?&gt;
<?php $this-&gt;widget('zii.widgets.CListView', array(
'dataProvider'=&gt;$dataProvider,
'itemView'=&gt;'_profilesview',
'template'=&gt;'{sorter}<br /&gt;{pager}{items}{pager}',
'enableSorting' =&gt; true,
'sortableAttributes'=&gt;array(
'name'=&gt;'By firstName',
'location'=&gt;'By city',
'age'=&gt;'By age',
'likes'=&gt;'By likes'
),
'afterAjaxUpdate'=&gt;'readcookie()',
?&gt;
my js function is
$(document).ready(function(){
function readcookie()
alert("hi");
i can see in my source that function defined in the file is included after all the js files that are included by yii default
when i register my script in my layout it dosent find $ because jquery not included when i include jquery it gets included twice which leads to trigger my event
i also tried to by setting
renderPartial('Mybelowview',null,false,true)
which leads again my js file to include multiple times and my event gots trigger multiple time.
This is very confusing please help to get out of it
Thanks all for being so generous
Comment: yes. but when i view source of the HTML page found that it is called after all the yii included JS file so i got error function undefined

Comment: @bool.dev now what i did, instead of calling the function i defined it there but i dont feel its a good way since i will have to define it every time i will use it anywhere else can u suggest me right way doing this

Comment: here is my code http://pastebin.com/embed_js.php?i=BiihjQxe thanks a lot.

Comment: you have included the js file with the `readcookie()` function in the layout or view?

Comment: show me the code of how you were doing it before, so that i can recreate it here and see what could have been wrong, how you were registering, and in which file/view/layout

Here is the accepted answer: The problem is that functions inside $(document).ready(); are out of scope outside of it, and that's why you get undefined. So you can either just have:
// $(document).ready(function(){
function readcookie()
alert("hi");
// });
// omit document.ready to make function available in the global scope
or define the function on the window object to make it global:
$(document).ready(function(){
window.readcookie=function ()
alert("hi");
Lastly define the attribute 'afterAjaxUpdate' as :
'afterAjaxUpdate'=&gt;'readcookie'
// if it is readcookie() it becomes a function call instead
Comment for this answer: be sure to follow up, if you have doubts or anything


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:06/09/12', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<jquery><ajax><yii><yii-components>', 'Title': 'afterAjaxUpdate callbackfunction CListView shows undefined', 'ViewCount': 1819, 'id': 10960829, 'user_AboutMe': '<p>web is amazing AND vast</p>\n', 'user_AccountId': 475285.0, 'user_DisplayName': 'Bipin Chandra Tripathi', 'user_Reputation': 2532.0, 'user_Views': 346.0, 'user_id': 885515.0}}