2010-12-29 4 views
0

SYNCHRON 전화로 ajaxSubmit : 다른 방법으로, 때로는jQuery를 : 나는 중앙 자바 스크립트 기능이

function storeProperties() { 
    $("form#systemProperties").ajaxSubmit({ 
     data: $("form#systemProperties").serialize(), 
     type: 'post', 
     url: '?url=super', 
     success: function(response) { 
      /* output & co */ 
     } 
    }); 
} 

이 방법은 때때로 그 자체로 불려가, 예 :

function sendMessage() { 
    storeProperties(); /* it's a precondition */ 

    /* let's do some magic */ 
} 

문제는 storeProperties()은 비동기식으로 완료되고 ajaxSubmit은 아직 처리됩니다. 불행히도 마법은 storeProperties()이 완료되기 전에 시작하면 안됩니다.

storeProperties() 통화를 동기화하거나 완료되면 즉시 두 번째 기능을 트리거 할 수 있습니까?storeProperties()을 호출하는 기능이 한 가지 더 많습니다.

감사합니다.

답변

1

마술 기능을 별도의 기능으로 설정하고 storeProperties에 매개 변수로 전달하십시오. 즉

function storeProperties(successFunc) 
{  
    $("form#systemProperties").ajaxSubmit 
    (
     {   
      data: $("form#systemProperties").serialize(),   
      type: 'post',   
      url: '?url=super',   
      success: function(response) 
      {    
       /* output & co */   
       if(successFunc && (typeof successFunc == 'function')) 
       { 
        successFunc(response); 
       } 
            else 
            { 
            //do the default stuff 
            } 
      }  
     } 
    ); 
} 

function sendMessage() 
{  
    storeProperties(function(data){doTheMagic(data);}); 
} 

function doTheMagic(data) 
{ 
    /* let's do some magic */ 
} 

편집 : 매개 변수 문제는 마법이 항상 발생하지 것을 암시하는 것 같다

+0

로 함수를받을 수 storeProperties 업데이트되었습니다. 나는. storeProperties()를 호출하면 – Jim

관련 문제