2012-03-06 190 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, 
     ...