简体   繁体   中英

How do I execute a JQuery anonymous function passed as an argument?

Having an issue with the code below. I performed a search on the site and came up with close but no cigar.

I want when the item attached to .callapp is clicked to do the following:

  1. $jQuery.load the content section with the url received from getURL(xapp)
  2. regardless of what the status is, execute the "requesthandler"
  3. if the request happens to be successful, for the "requesthandler" to use onSuccess and execute it

I am not sure how to execute the anonymous function I am passing to the "requesthandler". I need this anonymous function to execute the "loadURL" function with the params I passed in, which will then spike another chain of events that is irrelevant to this post.

Thanks!

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);
        }
    }
}
onSuccess = onSuccess | null;

You need to use two | here, not one.

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

Just to be safe, I'd check if onSuccess was a function (not just that it's not null) before running it:

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

If you do this, the onSuccess || null onSuccess || null isn't needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM