2012-07-24 113 views
0

對下面的代碼有問題。我在網站上進行了搜索,並提出了密切但沒有雪茄。如何執行作爲參數傳遞的JQuery匿名函數?

我要當連接到.callapp項目被點擊做到以下幾點:

  1. $ jQuery.load通過getURL(XAPP)接受
  2. 不管什麼地位與URL的內容部分是,執行「requesthandler」
  3. 如果請求恰好是成功的,對於「requesthandler」使用的onSuccess和執行它

我不知道如何執行匿名函數我傳遞給「請求處理程序」。我需要這個匿名函數來執行帶有我傳入的參數的「loadURL」函數,然後它會激起另一個與本文無關的事件鏈。

謝謝!

var url = ""; 
var tab = ""; 
var app = "base"; 
var out = ""; 
$(document).ready(function() { 
    $(".callapp").click(function(e) { 
     e.preventDefault(); 
     $('#wxreturn').html("<div id='xtools'></div><div id='xerror'></div><div id='xdapps'></div>"); 
     hideContent(1, 1); 
     $('#xdapps').load(getURL("base"), function(resp, stat, xhr) { 
      requesthandler(iStat, iXHR, 0, 0, 0, 0, function() { 
       loadURL("tools", "xtools", 1, 1, 1, 1); 
      }); 
     }); 
     return 0; 
    }); 
}); 
// piece together a url 
function getURL(xapp) { 
    // url to return 
    var url = null; 
    // global tab must not be empty 
    if (tab.length) { 
     // check if app is defined 
     if (!xapp.length) { 
      // app is either the global or base 
      xapp = app | "base"; 
     } else { 
      // set the global 
      if (!(xapp == "tools") && !(xapp == "options")) app = xapp; 
     } 
     // set the url to return 
     url = "/" + tab.toLowerCase() + "/" + xapp.toLowerCase() + "?_=" + Math.random(); 
    } else { 
     // undefined functionality error 
     alert("Invalid getURL...Tab Missing"); 
    } 
    // return the url 
    return url; 
} 
// load a url 
function loadURL(xapp, target, showApp, showOpt, showTools, clearTools) { 
    // defaults 
    showApp = showApp | 0; 
    showOpt = showOpt | 0; 
    showTools = showTools | 0; 
    clearTools = clearTools | 0; 
    // do only if app and target are defined 
    if (!(xapp == undefined) && !(target == undefined)) { 
     // set target 
     if (!(target.contains("#"))) target = "#" + target; 
     // get url string 
     var url = getURL(xapp); 
     // check if null 
     if (!(url == null)) { 
      // commence with load 
      $(target).load(url, function(resp, stat, xhr) { 
       // call back with the request handler 
       requesthandler(stat, xhr, showApp, showOpt, showTools, clearTools); 
      }); 
     } 
    } else { 
     // undefined functionality error 
     alert("Invalid LoadURL...Missing App...Target"); 
    } 
} 
// request handler 
function requesthandler(stat, xhr, showApp, showOpt, showTools, clearTools, onSuccess) { 
    // defaults 
    showApp = showApp | 0; 
    showOpt = showOpt | 0; 
    showTools = showTools | 0; 
    clearTools = clearTools | 0; 
    onSuccess = onSuccess | null; 
    // check for status 
    if (stat == "success") { 
     // execute 
     if (!(onSuccess == null)) { 
      // perform function 
      (onSuccess()); 
     } 
    } else { 
     if (xhr.status == 401) { 
      // throw session expired 
      sessionTimeOut(); 
     } else if (xhr.status == 403) { 
      // throw authorization failure 
      failedAuthorize(); 
     } else { 
      // throw application request failure 
      failedAPPRequest(clearTools); 
     } 
    } 
} 

回答

3
onSuccess = onSuccess | null; 

您需要使用兩個|這裏,沒有之一。

showApp = showApp || 0; 
showOpt = showOpt || 0; 
showTools = showTools || 0; 
clearTools = clearTools || 0; 
onSuccess = onSuccess || null; 

爲了安全起見,我會檢查是否onSuccess是一個函數(不只是它不爲空)運行前:

if (typeof onSuccess === 'function') { 
    // perform function 
    onSuccess(); // the extra "()" aren't needed here 
} 

如果你做到這一點,onSuccess || null不需要。

+0

那岩石!謝謝! – 2012-07-24 15:39:26

+0

不客氣:-D – 2012-07-24 15:39:56

相關問題