2013-11-22 3 views
0

이상한 문제가 있습니다. 나는 그것이 변수의 범위와 관련이 있다고 확신하지만, 왜 그럴 수없는 것인지 알 수 없다. 이것은 다음과 같은 코드를 구글 크롬 31.0.1650.57 및 Firefox 24.0에서 수행되고있다 : 먼저 template.js 다음 전화를 함수에 보낼 때 객체의 유형이 정의되지 않았습니다.

$("select[name=class]").change(function() { 
     var subclassSQL = new Array(); 
     subclassSQL[0] = "`class`, `type`, `subclass`"; 
     subclassSQL[1] = "subclass"; 
     subclassSQL[2] = "class"; 
     subclassSQL[3] = $("select[name=class] option:selected").html(); 
     splitArr = new Array(); 
     splitArr[0] = split; 
     splitArr[1] = "type"; 
     call(subclassSQL, "subClass", "div.subClass", (function(){split(); Handlebars.registerHelper("top", function(object) {return new Handlebars.SafeString("<span>Choose your " + object[0].type + ":</span>\n<select name=\"subClass\">");});Handlebars.registerHelper("bottom", function() {return new Handlebars.SafeString("</select>");});}), splitArr, false); 
    }); 

변경되는 드롭 다운에 의해 트리거됩니다 (다음 섹션 template.js의 라인 28에서 시작) 호출되는시 콘솔에서이 옵션을 선택하면 ajaxCalls.js의 기능
function call(SQLarray, source, container, helper, function_, run) { 
    $.ajax({ 
     url: "includes/ajaxHandler.php", 
     type: 'post', 
     dataType: 'json', 
     data: {info: SQLarray}, 
     success: function(json) { 
      var template = Handlebars.templates[source], 
       tempData = {base: json} 
      console.log(tempData.base); 
      if (helper) {helper();} 
      if (function_) { 
       $(container).html(template(function_[0](function_.slice(1), tempData))); 
      } else { 
       $(container).html(template(tempData)); } 
      if (run) {update(jQuery);} 
     } 
    }); 
}; 

function split(field, tempData) { 
    console.log(tempData.base); 
    return tempData; 
}; 

을 트리거, 첫 번째 console.log(tempData.base);는 예상대로 JSON 개체를 반환합니다. 하지만 두 번째 반환

Uncaught TypeError: Cannot read property 'base' of undefined ajaxCalls.js:22 
split              ajaxCalls.js:22 
(anonymous function)           template.js:37 
$.ajax.success            ajaxCalls.js:11 
c                jquery.js:3048 
p.fireWith             jquery.js:3160 
k                jquery.js:8235 
r                jquery.js:8778 

function_에 대한 호출의 구문은 다음과 같습니다 왜이 정의되지 않은를 반환

split("type", tempData); 

?

답변

0

해결책을 찾았습니다! 문제는 call 기능을 트리거하는 난독 화 된 라인 때문이었습니다.

필자는 몇 줄을 써서 필자는 spilled()라고 불렀다. hepler 기능 내에서. 하지만 변수를 보내지 않았습니다! split();을 제거하면 문제가 해결됩니다. 이것은 단지 보여주기 위해 간다. 기능을 배치하는 것이 중요합니다.

관련 문제