2012-03-06 3 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) { 

그리고 당신은 같은 사용 :

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

을 그리고 필요한 경우 다음 fn()을 사용 fn 전화 :

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

이 같이 당신의 기능을 수정합니다.

1

getTree 기능을 추가하십시오. 당신의 아약스 옵션에

function getTree(hash, callback) 

그리고 "jsopCallback"매개 변수를 사용하여 같은 뭔가

$.ajax({ 
     ... 
     jsopCallback: callback, 
     ... 
관련 문제