我制作了一些 jQuery 插件——它们都解析 JSON 提要并使用 Mustache.js 渲染它们;每个插件都采用一个整数值来表示要显示的项目数。
我Uncaught TypeError: Cannot read property 'link' of undefined
在尝试使用以下代码解析 Stack Overflow JSON 提要时收到一条消息:
$.ajax({
type: 'GET',
url: query,
contentType: "jsonp",
dataType: 'jsonp',
success: function (jsonp) {
/* loop through JSON items converting the time from UNIX timestamp
** format to readable words by parsing it through timeConverter() */
var i;
for (i = 0; i < jsonp.items.length; i++) {
if (i > num-1){
delete jsonp.items[i];
} else {
jsonp.items[i].creation_date = timeConverter(jsonp.items[i].creation_date);
}
}
var output = Mustache.render(must_template, jsonp);
element.html(output);
return element;
} //EOF CALLBACK
}); //EOF AJAX
作为一个快速修复,我通过简单地注释掉delete
操作来禁用截断。该错误表明 Mustache.js 正在尝试访问不再存在的 JSON 对象的一部分;然而,该delete
操作显然只影响超出用户定义限制的项目。
发生此行为时,数组中仍有 20 个项目。
注意: 是的,我自己回答了这个问题;但是 - 如果它显示最佳实践,更简洁的方式或以某种方式改进我的答案,我非常愿意接受另一个答案。:)