Reputation: 6276
I'm accessing an array stored as an object via jQuery's .data method but for some reason I cannot get the nested property in IE9
var $commentdata = $comment.data('comments'),
$position = $commentdata.playtime;
In every other browser it works fine. However in IE9 I get the following error:-
Error: Unable to get value of the property 'playtime': object is null or undefined
Any idea why this may be and how I can fix it?
EDIT: Here is the function that is adding the data to the dom:
function getComments(oTable) {
var $array = [];
if (oTable.find('.dataTables_empty').length === 0) {
//build array of songid's to send to server
oTable.find('td').each(function () {
"use strict";
var $songdata = $(this).data('data'),
$songid = $songdata.songid,
$duration = $songdata.duration;
$array.push($songid);
});
$.ajax({
type: "GET",
dataType: "json",
async: false,
url: "/wp-content/comment_data.php",
data: 'songid=' + $array,
success: function (data) {
"use strict";
$commentarray.length = 0;
$commentarray.push(data);
//apply template to each song and render comment icons
console.log($commentarray);
$.each(data, function (key, value) {
var $songdata = data[key];
if ($songdata !== null) {
var $songid = $songdata[0].songid,
songid = 'songid' + $songid,
$commentbar = $('#comment-bar-' + $songid),
sID = $commentbar.closest('td').find('a.playable').data('playable').sID,
song = soundManager.getSoundById(sID);
$commentbar.html($.render(data[key], 'commentBarTemplate'));
$.each($songdata, function (index, value) {
var commentdata = $songdata[index],
position = commentdata.position,
cid = commentdata.id,
$templateDiv = $('#timestamped-comment-template');
//$comment = $('#comment-' + cid).find('a');
//$comment.data('comments', commentdata);
song.onPosition(position * 1000, function () {
if ($('#example').find('td').hasClass('comments-shown') === false && $templateDiv.hasClass('hover') === false && $('body').attr('looping') === '') {
showNewComment($comment);
}
});
});
}
});
}
});
}
else {
return false;
}
}
I'm quite new to jQuery and am still getting my head around it so apologies if this code isn't great. But regardless all the data gets put into the DOM correctly and I said is acessible fine on every browser other than IE.
I have just noticed... It does only seem to fire the error when accessing comments that do not have any replies. bizarrely!
Here is a small portion of the JSON returned:
{ "songid22" : null,
"songid23" : [ { "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test",
"datasize" : "204",
"display_name" : "admin",
"id" : "5",
"playtime" : 27.450980392200002,
"position" : "56",
"posttime" : "2011-09-28 15:32:41",
"replies" : [ null ],
"songid" : "23",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test comment",
"datasize" : "204",
"display_name" : "admin",
"id" : "6",
"playtime" : 42.647058823499997,
"position" : "87",
"posttime" : "2011-09-28 16:38:37",
"replies" : [ null ],
"songid" : "23",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
}
],
"songid24" : [ { "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "comment at 0:48",
"datasize" : "267",
"display_name" : "admin",
"id" : "2",
"playtime" : 17.977528089900002,
"position" : "48",
"posttime" : "2011-09-28 14:38:41",
"replies" : [ { "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "haha reply",
"cid" : "2",
"display_name" : "admin",
"id" : "1",
"posttime" : "2011-09-28 15:14:56",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "next replty",
"cid" : "2",
"display_name" : "admin",
"id" : "2",
"posttime" : "2011-09-28 15:15:07",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "first reply",
"cid" : "2",
"display_name" : "admin",
"id" : "3",
"posttime" : "2011-09-28 15:15:15",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
}
],
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "asdasd",
"datasize" : "267",
"display_name" : "admin",
"id" : "3",
"playtime" : 0.74906367041199995,
"position" : "2",
"posttime" : "2011-09-28 14:43:11",
"replies" : [ { "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test reply",
"cid" : "3",
"display_name" : "admin",
"id" : "5",
"posttime" : "2011-10-06 14:35:23",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.example.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test reply",
"cid" : "3",
"display_name" : "admin",
"id" : "6",
"posttime" : "2011-10-07 14:48:21",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
}
],
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.example.com/members/admin/"
},
{ "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
"body" : "test",
"datasize" : "267",
"display_name" : "admin",
"id" : "10",
"playtime" : 36.329588014999999,
"position" : "97",
"posttime" : "2011-10-06 14:15:12",
"replies" : [ null ],
"songid" : "24",
"uid" : "1",
"user_login" : "admin",
"user_url" : "http://www.songbanc.com/members/admin/"
}
],
My comment retrieval works fine on all comments that have a further 'replies' nest. if replies = null then IE fires the error.
EDIT2: From further investigation using firebug Lite on IE I can see that commentdata is still an object as it should be, but that it is completely empty on ann comments that do NOT have nested 'replies' in their JSON structure. Therefore it seems that the data is not getting connected to the dom correctly and that the fault must be my code above, not connecting the array correctly in comments without replies. Strange that it works fine on all other browsers however.
Any more ideas?
Upvotes: 0
Views: 634
Reputation: 6276
Problem solved... Believe it or not it turned out to be a missing closing tag in my template.
Shocking that I missed it although it's kind of surprising that it would throw IE 9 off as much as it did.
My apologies for wasting peoples time.
Cheers
Upvotes: 1