简体   繁体   English

如何编写一个接受回调作为参数的jquery函数

[英]How do I write a jquery function that accepts a callback as a parameter

I Have the following function. 我有以下功能。

function ChangeDasPanel(controllerPath, postParams) {

    $.post(controllerPath, postParams, function(returnValue) {

        $('#DasSpace').hide("slide", { direction: "right" }, 1000, function() {

            $('#DasSpace').contents().remove();

            $('#DasSpace').append(returnValue).css("display", "block");

            $('#DasSpace').show("slide", { direction: "right" }, 1000);

        });

    });

};

But I want to be able to call it like this 但我希望能够像这样称呼它

ChangeDasPanel("../Home/Test", {} ,function (){
  //do some stuff on callback
}

How can I implement support for callbacks in my function? 如何在我的函数中实现对回调的支持?

function ChangeDasPanel(controllerPath, postParams, f) {
  $.get(
    controllerPath, 
    postParams, 
    function(returnValue) {
      var $DasSpace = $('#DasSpace');
      $DasSpace.hide(
        "slide", { direction: "right" }, 1000, 
        function() {
          $DasSpace.contents().remove();
          $DasSpace.append(returnValue).css("display", "block");
          $DasSpace.show("slide", { direction: "right" }, 1000);
        }
      );
      if (typeof f == "function") f(); else alert('meh');
    }
  );
};

You can pass functions like any other object in JavaScript. 您可以像JavaScript中的任何其他对象一样传递函数。 Passing in a callback function is straight-forward, you even do it yourself in the $.post() call. 传入回调函数非常简单,您甚至可以在$.post()调用中$.post()

You can decide whether you want to have your callback called as part of the $.post() callback or on its own. 您可以决定是否要将回调作为$.post()回调的一部分进行调用,或者$.post()调用。

You know that global variables and functions are evil, so why not put your's into the jQuery namespace: 你知道全局变量和函数都是邪恶的,所以为什么不把你的内容放到jQuery命名空间中:

$.extend({
  myFunc : function(someArg, callbackFnk){
    var url = "http://example.com?q=" + someArg;
    $.getJSON(url, function(data){

      // now we are calling our own callback function
      if(typeof callbackFnk == 'function'){
        callbackFnk.call(this, data);
      }

    });
  }
});

$.myFunc(args, function(){
  // now my function is not hardcoded
  // in the plugin itself
});

Read this post to get a better understanding: Creating callback functions in jQuery 阅读这篇文章以便更好地理解: 在jQuery中创建回调函数

If I understand you correctly, it's as easy as setting another parameter and calling the variable as a function: 如果我理解正确,就像设置另一个参数并将变量调用为函数一样简单:

function foo(mycallback) {
    mycallback();
}

Why don't you use an object that you can pass through to the function. 为什么不使用可以传递给函数的对象。 It is much more jQuery like and it saves you having x named parameters which is hard to maintain as it can get unwieldy when you get past 3 params. 它更像jQuery,它可以节省你的x命名参数很难维护,因为当你超过3个参数时它会变得笨拙。

eg 例如

function callingFunction(){

      var fnSettings: {
        url: someUrl,
        data: params,
        callback : function(){}
      };


      yourFunction( fnSettings );

}

function yourFunction( settings ) {

      $.post( settings.url, settings.data, settings.callback );

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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