I made it, but not all works as I expect.
I run in my template:
<script type="text/javascript" src="<?php echo $baseUrl . DIRECTORY_SEPARATOR . $app_params['work_js_dir'].'/admin/category.js' ?>"></script>
<script>
var CategoryFuncs = new CategoryFuncs({
baseUrl : '<?php echo Yii::app()->request->baseUrl; ?>',
YII_CSRF_TOKEN : '<?php echo Yii::app()->request->csrfToken ?>'
});
CategoryFuncs.Test();
</script>
and in source code of the page I see what I expect:
<script type="text/javascript" src="/testdrive/work_js/admin/category.js"></script>
<script>
var CategoryFuncs = new CategoryFuncs({
baseUrl : '/testdrive',
YII_CSRF_TOKEN : 'a4ab9159a9a65b2041033e8df40a804f10b928ac'
});
// obj.doStuff();
CategoryFuncs.speak();
</script>
but problem is that when I show
///////////////////////////////////////////////////////////////
Thanks, it works but not fully.
In view I init TourFuncs object:
<script type="text/javascript" src="<?php echo $baseUrl . DIRECTORY_SEPARATOR . $app_params['work_js_dir'].'/admin/tour.js' ?>"></script>
<script>
var TourFuncs = new TourFuncs({
tour_id : '<?php echo $tour_id; ?>',
baseUrl : '<?php echo $baseUrl; ?>',
YII_CSRF_TOKEN : '<?php echo Yii::app()->request->csrfToken ?>'
});
TourFuncs.Test();
</script>
/admin/tour.js:
function TourFuncs(Params) {
this.tour_id = Params.tour_id;
this.baseUrl = Params.baseUrl;
this.YII_CSRF_TOKEN = Params.YII_CSRF_TOKEN;
alert( "this.YII_CSRF_TOKEN::" + var_dump(this.YII_CSRF_TOKEN) + " this.baseUrl::" + this.baseUrl +" this.tour_id::" + this.tour_id ) // ENTERED PARAMETERS HAVE VALID VALUES
}
TourFuncs.prototype.Test = function () {
alert("TourFuncs.prototype.Test my baseUrl is : " + this.baseUrl + "this.YII_CSRF_TOKEN::" + this.YII_CSRF_TOKEN); // ENTERED PARAMETERS HAVE VALID VALUES
}
...
TourFuncs.prototype.Tour_HighlightEdit = function (id) {
alert( "Tour_HighlightEdit id::" + id )
$("#tour_highlight_name").val('')
$("#tour_highlight_edit_dialog").dialog({
buttons: {
"Submit": function () {
var tour_highlight_name = $("#tour_highlight_name").val()
if (tour_highlight_name == "") {
alert("Enter name !")
$('#tour_highlight_name').focus();
return;
}
var HRef = this.baseUrl + "/tour/change_tour_highlight" // this.baseUrl = UNDEFINED
alert( "HRef::" + HRef)
var DataArray = {
"tour_id": this.tour_id,
"tour_highlight_id": "",
"name": encodeURIComponent(tour_highlight_name),
'YII_CSRF_TOKEN': this.YII_CSRF_TOKEN // this.YII_CSRF_TOKEN = UNDEFINED
};
alert( "::" + var_dump(DataArray) )
jQuery.ajax({
url: HRef,
type: "POST",
data: DataArray,
success: onChangedTourHighlight,
dataType: "json"
});
},
"Close": function () {
$(this).dialog("close");
}
},
hide: 'slide',
show: 'slide',
modal: true,
height: 520,
width: 720
}).dialog("open");
}
The constructor of TourFuncs and method TourFuncs.Test are called during the opening of the page and they work ok.
But this page also has list of related objects, which is loaded by ajax, and loading this page button "new" is visible.
Clicking on it ajax dialog page is opened and user can enter new value( function TourFuncs.Tour_HighlightEdit is called ). When user click "Save" vars this.baseUrl and
this.YII_CSRF_TOKENare= UNDEFINED. I tried to move vars this.YII_CSRF_TOKENare before definition of class TourFuncs and it works ok.
But I do not think that is the good idea make the variables public for all file.