我已经做了一些函数来使用Github API检索数据。我有回调来获取数据,但我确定如何理解函数退出的位置以及何时停止修改数据。试图通过填写对象来了解函数和回调范围
例如,在下面的代码中,在第一个函数中,当AJAX调用成功时,回调在第二个函数中被执行,其中数据被操纵。这是否意味着第一个函数的返回是不需要或使用的?在第二个函数中,数据被使用并推送到数组,然后数组返回,或者返回(空)数组返回的位置,然后回调完成它的工作。
我最终试图从回调中获取数据到对象中,并从父函数返回填充对象。
function makeAJAXCall(hash, cb) {
var returnedJSON, cb = cb, hash = hash;
$.ajax({
accepts: 'application/vnd.github-blob.raw',
dataType: 'jsonp',
url: hash,
success: function (json) {
console.info(json);
returnedJSON = json;
// Time for callback to be executed
if (cb) {
cb(json);
}
},
error: function (error) {
console.error(error);
// an error happened, check it out.
throw error;
}
});
return returnedJSON;
}
function parseBlob(hash) {
var objectedJSON, objectList = [], i;
objectedJSON = makeAJAXCall(hash, function (objectedJSON) { // no loop as only one entry
objectList.push(objectedJSON.content);
});
return objectList;
}
function walkTree(hash) {
var objectedJSON, objectList = [], i, entry;
var hash = 'https://api.github.com/repos/myAccountName/repo/git/trees/' + hash;
objectedJSON = makeAJAXCall(hash, function (objectedJSON) {
for (i = 0; i < objectedJSON.data.tree.length; i += 1) {
entry = objectedJSON.data.tree[i];
console.debug(entry);
if (entry.type === 'blob') {
if (entry.path.slice(-4) === '.svg') { // we only want the svg images not the ignore file and README etc
console.info(entry.path)
objectList.push(parseBlob(entry.url));
}
} else if (entry.type === 'tree') {
objectList.push(walkTree(entry.sha));
}
}
});
console.info(objectList);
return objectList;
}
$(document).ready(function() {
var objects = walkTree('master', function() { // master to start at the top and work our way down
console.info(objects);
});
});
你能指出一些正确的事情做的例子,我可以尝试解决这个问题吗? – Hyposaurus 2012-03-16 01:42:31