2012-03-06 113 views
0

我有以下代码从github中获取JSON对象,并且正在尝试将某些部分添加到数组中。回调函数似乎没有执行

function getTree(hash) { 
    var pathToTree, returnedJSON; 
    pathToTree = 'https://api.github.com/repos/myaccount/myrepo/git/trees/' + hash; 
    $.ajax({ 
     accepts: 'application/vnd.github-blob.raw', 
     dataType: 'jsonp', 
     url: pathToTree, 
     success: function (json) { 
      returnedJSON = json; 
     }, 
     error: function (error) { 
      console.debug(error); 
     } 
    }); 
    return returnedJSON; 
} 

function parseTree(hash) { 
    var objectedJSON, objectList = [], i, entry; 
    objectedJSON = getTree(hash, function() { 
     console.debug(objectedJSON);     // this is not appearing in console 
     for (i = 0; i < objectedJSON.data.tree.length; i += 1) { 
      entry = objectedJSON.data.tree[i]; 
      console.debug(entry); 
      if (entry.type === 'blob') { 
       if (entry.type.slice(-4) === '.svg') {  // we only want the svg images not the ignore file and README etc 
        objectList.append(i.content); 
       } 
      } else if (entry.type === 'tree') { 
        objectList.append(parseTree(getTree(entry.sha))); 
      } 
     } 

    }); 
    return objectList; 
} 

$(document).ready(function() { 
    var objects = parseTree('master', function() { 
     console.debug(objects); 
    }); 
}); 

我的代码检索JSON对象不错,但我试图把它解析(又名拉出我想要的位)时遇到了麻烦。我正在使用的回调似乎没有进行,并想知道是否有人可以看看它并帮助我。

具体来说,我可以添加一个回调到我选择的任何函数吗?我必须为这个功能做什么吗?

回答

1

我已修复代码来说明如何去做。

function getTree(hash, cb) { 
    // notice that I copy the callback and hash references to have access to them in this 
    // function's closure and any subsequent closures, like the success and error 
    // callbacks. 
    var pathToTree, returnedJSON, cb = cb, hash = hash; 
    pathToTree = 'https://api.github.com/repos/myaccount/myrepo/git/trees/' + hash; 
    $.ajax({ 
     accepts: 'application/vnd.github-blob.raw', 
     dataType: 'jsonp', 
     url: pathToTree, 
     success: function (json) { 
      returnedJSON = json; 
      // if anything was passed, call it. 
      if (cb) cb(json); 
     }, 
     error: function (error) { 
      console.debug(error); 
      // an error happened, check it out. 
      throw error; 
     } 
    }); 
    return returnedJSON; 
} 

function parseTree(hash) { 
    var objectedJSON, objectList = [], i, entry; 
    objectedJSON = getTree(hash, function (objectedJSON) { 
     console.debug(objectedJSON);     // this is not appearing in console 
     for (i = 0; i < objectedJSON.data.tree.length; i += 1) { 
      entry = objectedJSON.data.tree[i]; 
      console.debug(entry); 
      if (entry.type === 'blob') { 
       if (entry.type.slice(-4) === '.svg') {  // we only want the svg images not the ignore file and README etc 
        objectList.append(i.content); 
       } 
      } else if (entry.type === 'tree') { 
        objectList.append(parseTree(getTree(entry.sha))); 
      } 
     } 

    }); 
    return objectList; 
} 

$(document).ready(function() { 
    var objects = parseTree('master', function() { 
     console.debug(objects); 
    }); 
}); 
1

至于我可以看到它,你是不是传递回调到您的功能:

function getTree(hash) { 

并且使用的是这样的:

objectedJSON = getTree(hash, function() { 

同样此功能不会有所回调PARAM:

function parseTree(hash) { 

并且使用的是这样的:

var objects = parseTree('master', function() { 

修改你的功能是这样的:

function getTree(hash, fn) { ... } 
function parseTree(hash, fn) { ... } 

,然后在需要的时候使用fn()调用fn

1

添加第二个参数o getTree函数。喜欢的东西

function getTree(hash, callback) 

,并使用 “jsopCallback” 参数在你的Ajax选项

$.ajax({ 
     ... 
     jsopCallback: callback, 
     ...