Hey Antonio,
thanks for your hint. I tried several things, so if you like, let’s talk about it, because I detected some awful behaviours, if I did not some mistakes. I think my approach here is not unusual, so I still wonder, why is it such a circumstance :-\
I present you now my analysis and checkings:
controller-code:
class CalendarController extends controller {
public function actionShowWeekDates(){
$criteria = new CDbCriteria();
$criteria->condition = 'employee_id=2';
$appointments = Appointment::model()->findAll($criteria);
for ($i = 0; $i != sizeof($appointments); $i++){
$appointments[$i] = $appointments[$i]->toJSON(); //I use the EJsonBehavior.php!
}
$appointments = CJavaScript::jsonEncode($appointments);
echo $appointments;
}
}
My JS-Code inside the Page (i have a very javascript-based calendar, so i need to use javascript without any JS->PHP interaction)
JS-Function-Code Ajax-Code (I use jQuery 1.5!):
$.ajax({
url: 'calendar/showWeekDates',
dataType: 'json',
data: {
/*....*/
},
complete: function(jqXHR, textStatus){
var response = jqXHR.responseText;
/*
first try to encode with JSON from json.org
http://json.org/js
json.js: https://github.com/douglascrockford/JSON-js/blob/master/json.js
*/
var JSONObject = JSON.parse(response);
/*
second try to encode with jQuery parseJSON
http://api.jquery.com/jQuery.parseJSON/
*/
var JSONjQueryObject = jQuery.parseJSON(response);
}
});
So, now comes the big battle. Now I expect a full qualified JavaScript Object to access the datas inside.
My Response:
[
"{'attributes':{'id':'1','date':'2011-02-23 14:00:00','customer_id':'1','task_id':'1','employee_id':'1'},'relations':{'customer':{'id':'1','first_name':'Sandra','last_name':'Bullock','email':'sandra@bullock.com'},'task':{'id':'1','name':'Massage'},'employee':{'id':'1','name':'Marco','email':'email@provider.com'}}}"
]
But neither the JSON.parse() nor the jQuery.parseJSON() results in an JavaScript-Object to access the data!!!
I’m a bit dissappointed from Yii, that seems to be a basic approach from behaviour with ajax, json and requesting the controller’s data. What I’m doing wrong, what can I do better?
My temporarily Solution to have a JavaScript-Object to access data is now:
$.ajax({
url: 'calendar/showWeekDates',
dataType: 'json',
data: {
/*....*/
},
complete: function(jqXHR, textStatus){
var response = jqXHR.responseText;
var JSONObject = jQuery.parseJSON(response);
var evaledJson = eval('(' + JSONObject[0] + ')');
}
});
And now with the additional parsing via “eval(’(’ + JSONObject[0] + ‘)’)” I can access the full object for example the task-name: “alert(evaledJson.relations.task.name);”!
In case of this Problem, I tried to research for this problem. But it was difficult, because it seems to be only a Yii-"Problem" or better so say behaviour.
I googled a lot and I think the response datas are not that correct that json-parser and jquery json-parser needed to be, so If i pass the result via http://www.jsonlint.com/ the result seems to be correct
!
Why I’m thinking the parsed result JSON-Structure (caused by Controllers “toJSON()”-Method in the for-loop) is that the very most information that I google (e.g. this google result) the JSON-Result-Structure seems to be a little different! For Example, If I take this kind of JSON-syntax would expect the response-result above to be like:
{
"attributes" : [
{
"id": "1",
"date": "2011-02-23 14:00:00",
"customer_id": "1",
"task_id": "1",
"employee_id": "1"
}
],
"relations" : [
{
"customer": [
{
"id": "1",
"first_name": "Sandra",
"last_name": "Bullock",
"email": "sandra@bullock.com"
}
],
"task": [
{
"id": "1",
"name": "Massage"
}
],
"employee": [
{
"id": "1",
"name": "Marco",
"email": "email@provider.com"
}
]
}
]
}
again my original Response was:
[
"{'attributes':{'id':'1','date':'2011-02-23 14:00:00','customer_id':'1','task_id':'1','employee_id':'1'},'relations':{'customer':{'id':'1','first_name':'Sandra','last_name':'Bullock','email':'sandra@bullock.com'},'task':{'id':'1','name':'Massage'},'employee':{'id':'1','name':'Marco','email':'email@provider.com'}}}"
]
So, with this knowledge I pasted the "new" SELF MADE Json-Syntax in my Ajax-Response (not the original response - I override and "fake" it):
$.ajax({
url: 'calendar/showWeekDates',
dataType: 'json',
data: {
/*....*/
},
complete: function(jqXHR, textStatus){
var response = "{\"attributes\" : [ {\"id\":\"1\",\"date\":\"2011-02-23 14:00:00\",\"customer_id\":\"1\",\"task_id\":\"1\",\"employee_id\":\"1\"}], \"relations\" : [ {\"customer\": [ {\"id\":\"1\",\"first_name\":\"Sandra\",\"last_name\":\"Bullock\",\"email\":\"sandra@bullock.com\"}], \"task\": [ {\"id\":\"1\",\"name\":\"Massage\"}], \"employee\":[ {\"id\":\"1\",\"name\":\"Marco\",\"email\":\"email@provider.com\"} ] } ] }";
var JSONObject = JSON.parse(response); //this way works
var JSONjQueryObject = jQuery.parseJSON(response); //jQuery way works, too
}
});
and in THIS (both) CASEs, I have directly the Javascript-Object I needed and can access e.g. via "alert(JSONObject.relations[0].task[0].name);" or (jQuery-way): "alert(JSONjQueryObject.relations[0].task[0].name);"
I hope I’m right and you don’t loose the overview, I can split it again a bit to discuss over it in detail, but I would be very pleased if I can have a reliable solution for this case.
Maybe Yii can be optimized with this handling. Again I’m surprised in the case, that I’m doing no mistakes, that no one else was crashing in this problem/question.
Thank you very much!