2013-06-14 3 views
0

사용자가 작업을 새로운 시간으로 드래그하면 AJAX가 PHP 컨텐츠를 다시로드하고 마지막으로 드래그 할 수있는 요소를 다시 초기화하도록 달력 타임 라인보기를 작성하고 있습니다. 작업을 다시 끌 수 있습니다. 아래의이 솔루션은 적은 호출을 줄일 수 있습니다Jquery UI 순서를 단순화하는 방법

내 질문은 그래서

내 질문은, 때문에 함수를 호출하는 모든 다른 방법을 과소하지 않도록 가능성이?

코드를 줄이고 "hierarchy"/ 내장 속성을 올바르게 사용하기 위해 "double"호출을 피하고 싶습니다.

드래그 {
---- {
------- 성공 AJAX {{리로드 기능 초기화 {}} // ---- 단부 AJAX} // 최종 성공
대화 ------- 취소 {AJAX {리로드 기능 {// 끝 AJAX ----}} 초기화} // 끝이
}

다음

가있다
---} 취소 현재 연결된 솔루션, 작동 중 :

이 식별자 대신의를 통해 당신이 명명 한 다음 손으로 모든 익명 함수

variable: function() {...} 

을 대체 할 수

$("#dialog-mjob").dialog({ 
    autoOpen: true, 
    resizable: true, 
    modal: true, 
    buttons: { 
     "Modify": function() { 
      $(this).dialog("close"); 
      $.ajax({ 
       type: "POST", 
       url: "get-event.php", 
       data: $('#mod-event-form').serialize(), 
       success: function (data) { } 
      }); 
     }, 
     Cancel: function() { 
      $(this).dialog("close"); 
      $.ajax({ 
       type: "POST", 
       url: "get-event.php", 
       data: $('#mod-event-form').serialize(), 
       success: function (data) { 
        $('#cal-inner').remove(); 
        $('#cal').load('timeline-inner.php', function() { 
         $(".draggable").draggable({ 
          //revert: true, 
          revert: "invalid", 
          //helper: "clone", 
          cursor: "hand", 
          grid: [44, 0], 
          axis: "x", 
          handle: "span", 
          cursor: "move", 
          stop: function (ev, ui) { 
           changePrompt($(this).attr('id')); 
          } 
         }); 
        }); 

       } 
      }); // end ajax 
     } // end cancel button 
    } // end buttons 
}); // end dialog 
+1

엄청난 들여 쓰기가 엉망입니다. 낯선 사람들이 코드 수정을 돕기를 원한다면, 적어도 그들이 읽는 것을 쉬워 보이지 않는가? – meagar

+0

죄송합니다. 빈약하게, 내가 무엇을 할 수 있는지 보도록하겠습니다. – zzipper72

+0

ok .. 조금 정리 했으므로 기능 순서에 대한 생각은 없습니까? – zzipper72

답변

1

참고.

variable: myFunc 
... 
function myFunc() {...} 

이렇게하면 늘어나는 코드 중첩을 용이하게 할 수 있으며 동일하거나 유사한 기능을 다시 사용할 수 있습니다.

$("#dialog-mjob").dialog({ 
    autoOpen: true, 
    resizable: true, 
    modal: true, 
    buttons: { 
     "Modify": function() { 
      $(this).dialog("close"); 
      $.ajax({ 
       type: "POST", 
       url: "get-event.php", 
       data: $('#mod-event-form').serialize(), 
       success: myAjaxFunction // <-- CHANGED! 
      }); 
     }, 
     Cancel: function() { 
      $(this).dialog("close"); 
      $.ajax({ 
       type: "POST", 
       url: "get-event.php", 
       data: $('#mod-event-form').serialize(), 
       success: myAjaxFunction // <-- CHANGED! 
      }); // end ajax 
     } // end cancel button 
    } // end buttons 
}); // end dialog 

// the new named function 
function myAjaxFunction (data) { 
    $('#cal-inner').remove(); 
    $('#cal').load('timeline-inner.php', function() { 
     $(".draggable").draggable({ 
      //revert: true, 
      revert: "invalid", 
      //helper: "clone", 
      cursor: "hand", 
      grid: [44, 0], 
      axis: "x", 
      handle: "span", 
      cursor: "move", 
      stop: function (ev, ui) { 
      changePrompt($(this).attr('id')); 
     } 
    }); 
} 

이 더욱 할 수 있습니다 : 이중 통화로

$("#dialog-mjob").dialog({ 
    autoOpen: true, 
    resizable: true, 
    modal: true, 
    buttons: { 
     "Modify": doModify, // <-- CHANGED! 
     Cancel: doCancel // <-- CHANGED! 
    } // end buttons 
}); // end dialog 

function doModify() {  // <-- NEW! 
    $(this).dialog("close"); 
    $.ajax({ 
     type: "POST", 
     url: "get-event.php", 
     data: $('#mod-event-form').serialize(), 
     success: myAjaxFunction 
    }); 
} 

function doCancel() {  // <-- NEW! 
    $(this).dialog("close"); 
    $.ajax({ 
     type: "POST", 
     url: "get-event.php", 
     data: $('#mod-event-form').serialize(), 
     success: myAjaxFunction 
    }); // end ajax 
} 

function myAjaxFunction (data) { // <-- NEW! 
    $('#cal-inner').remove(); 
    $('#cal').load('timeline-inner.php', function() { 
     $(".draggable").draggable({ 
      //revert: true, 
      revert: "invalid", 
      //helper: "clone", 
      cursor: "hand", 
      grid: [44, 0], 
      axis: "x", 
      handle: "span", 
      cursor: "move", 
      stop: function (ev, ui) { 
      changePrompt($(this).attr('id')); 
     } 
    }); 
} 

난 당신이 무슨 뜻인지 정말 확실하지 오전하지만 버튼이 제출되는 것을 두려워하는 경우 두 배 더 높은 설정할 수 있습니다 scoped 변수는 버튼이 이미 눌러 졌음을 나타냅니다.

+0

이 훌륭한 제안은 "성장하는 중첩 된 기능"을 다루었 기 때문에 내가 무엇을 찾고 있었는지, 무엇을 묻는 지 확신 할 수 없었지만 내가 어디로 향하고 있는지를 보았습니다. double에 관해서는, "중첩 (nests)"때문에이 문제를 다시 지적합니다 (코드를 계속 사용하면 중복을 두려워합니다). 정말 고마워요. – zzipper72

+0

능력이 있다면 전적으로 투표 할 수 있습니다. 어쩌면 누군가가 친절하고 충분히 대답 할 수있을뿐만 아니라 2 가지 수준의 더 나은 구문을 사용하기 때문에 가능할 것입니다. 다시 한 번 감사드립니다! – zzipper72

관련 문제