2012-12-22 1 views
0

여기에 전체 기능을 붙여 넣기하고, 길에서 나는이 스크립트에서 콧수염 템플릿 라이브러리를 사용하지만이 문제의 필요가 없습니다 :Ajax가 포함 된 함수 호출을 수행하는 더 짧은 방법이 있습니까?

tmplReplaceContent : function(json, tmpl, target){ 
    var regex = new RegExp("\{{[a-zA-Z\.\_]*\}}"); 
    var template = ''; 
    var view = ''; 
    /* json -> check if object */ 
    if (typeof json == 'object') { 
     view = json; 
     if(!regex.test(tmpl)){ 
      /* get mustache tmpl from the path */ 
      $.get(msi.vars.tmpl_url + tmpl + '.mustache', function(tmplOut){ 
       template = tmplOut; 
       var content = Mustache.render(template, view); 
       $(target).html(content).hide().fadeIn(); 
      }); 
     } else { 
      template = tmpl; 
      var content = Mustache.render(template, view); 
      $(target).html(content).hide().fadeIn(); 
     } 
    } else { 
     /* getJSON from the path */ 
     $.getJSON(msi.vars.base_url + json, function(jsonOut){ 
      view = jsonOut; 
      if(!regex.test(tmpl)){ 
       /* get mustache tmpl from the path */ 
       $.get(msi.vars.tmpl_url + tmpl + '.mustache', function(tmplOut){ 
        template = tmplOut; 
        var content = Mustache.render(template, view); 
        $(target).html(content).hide().fadeIn(); 
       }); 
      } else { 
       template = tmpl; 
       var content = Mustache.render(template, view); 
       $(target).html(content).hide().fadeIn(); 
      } 
     }); 
    } 

나는 그것이 짧은 만들 수 없습니다 그리고 비동기이기 때문에 Ajax 성공에 로컬 변수를 할당 할 수 없기 때문에 반복적 인 코드를 제거해야한다. 나는 약 15 시간 동안 인터넷에서 걸어 돌아 다녔다. 그러나 여전히 운이 없다.

반복 코드를 제거하고이 코드를 더 짧게 만들 수 있습니까?

답변

1
tmplReplaceContent : function(json, tmpl, target) { 

    var regex = new RegExp("\{{[a-zA-Z\.\_]*\}}"), 
     view = ''; 

    function setOutput(template) { 
     var content = Mustache.render(template, view); 
     $(target).html(content).hide().fadeIn(); 
    } 

    function doJSON(json) { 
     view = json; 
     if(!regex.test(tmpl)){ 
      /* get mustache tmpl from the path */ 
      $.get(msi.vars.tmpl_url + tmpl + '.mustache', setOutput); 
     } else { 
      setOutput(tmpl); 
     } 
    } 

    /* json -> check if object */ 
    if (typeof json === 'object') { 
    doJSON(json); 
    } else { 
     /* getJSON from the path */ 
     $.getJSON(msi.vars.base_url + json, doJSON); 
} 
+0

의도적으로 setOutput 및 doJSON에 매개 변수를 추가하지 않았습니까? –

+1

$ .getJSON & $ .get 성공 함수를 매핑하기 위해 선택된 두 가지 매개 변수가 있습니다. – closure

1

당신이 코드 : 중복 한 경우 글쎄, 함수가 당신은 하나를 포함 할 방법을 여기에 그냥 재미로에 대한

이있다 :

tmplReplaceContent : function(json, tmpl, target){ 

    function render(tmplOut) { 
     template = tmplOut; 
     var content = Mustache.render(template, view); 
     $(target).html(content).hide().fadeIn(); 
    } 

    var regex = new RegExp("\{{[a-zA-Z\.\_]*\}}"); 
    var template = ''; 
    var view = ''; 
    /* json -> check if object */ 
    if (typeof json == 'object') { 
     view = json; 
     if(!regex.test(tmpl)){ 
      /* get mustache tmpl from the path */ 
      $.get(msi.vars.tmpl_url + tmpl + '.mustache', render); 
     } else { 
      render(); 
     } 
// Etc... 

바로 물건을 꽤 많이 단축?