什么时候使用JQuery.Callbacks? [英] When would I use JQuery.Callbacks?

查看:78
本文介绍了什么时候使用JQuery.Callbacks?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览jQuery 1.7中添加的新内容,发现它们现在具有jQuery.Callbacks() http://api.jquery.com/jQuery.Callbacks/.

I was looking through new stuff added to jQuery 1.7 and I saw they now have jQuery.Callbacks() http://api.jquery.com/jQuery.Callbacks/.

该文档向您展示了如何使用jQuery.callbacks(),但没有提供有关何时使用它们的任何适用示例.

The documentation shows you how to use jQuery.callbacks() but not any applicable examples of when I would want to use them.

似乎您可以从回调列表中添加/删除回调,并且可以执行jQuery.callbacks().fire(args),但这只会触发该列表中的所有回调.也许我错过了一些东西,但这似乎不是很有用.

It seems you can add/remove callbacks from a callbacks list and you can do jQuery.callbacks().fire(args), but this just fires off ALL of the callbacks in that list. Maybe I am missing something but this doesn't seem very useful.

当我第一次看到此新功能时,我以为您可以将其与键/值对一起使用.然后,这将提供一种简单的方法来在应用程序中的单个位置管理回调函数.像

In my head when I first saw this new functionality I thought you would be able to use it with key/value pairs. Which would then provide a simple way to manage callback functions in a single place in your application. Something like

$.callbacks.add("foo", myFunction);

然后例如,如果我想在函数末尾调用该回调,我可以做类似的事情

and then for example if I wanted to call that callback at the end of my function I could do something like

$.callbacks().fire("foo", args);

但是,您似乎无法触发特定的回调,您只能使用给定的参数触发所有回调,或者不触发任何回调.

However it doesn't look like you can fire off specific callbacks, you can only fire off all of them with the given arguments or none of them.

我所看到的最接近的东西是能够为.fire()函数提供设置"this"属性的上下文

The closest thing I saw was being given the ability to give the .fire() function a context to set the "this" property

.fireWith(context, args)

但这实际上也无济于事.

but this doesn't really help much either.

  1. 我误解了文档吗?

如果这是所需的功能,那么可以使用一些有用的示例.

推荐答案

要在

To expand on @Rockets answer a bit and clear up some confusion:

可能需要使用jQuery的$.Callbacks的原因是多方面的:

The reason that one might need to use jQuery's $.Callbacks is multifaceted:

  1. 用户在一个函数中有很多代码,想要对其进行拆分
  2. 他们获取该信息并通过jQuery回调函数发送该信息,然后使他们能够将其代码拆分为更好的可管理部分,以供使用.
    因此(例如),如果您查看
    @Rocket的代码:

  1. The user has a lot of code in one function and wants to split it up
  2. They take that information and send it through the jQuery callback function which then allows them to have split their code into better manageable pieces with which to work with.
    So (for example) if you look at @Rocket's code:

var clickCallbacks = $.Callbacks();

clickCallbacks.add(function() { //one one function piece
    //parse and do something on the scope of `this`
    var c = parseInt(this.text(), 10);
    this.text(c + 1);
});
clickCallbacks.add(function(id) { //add a second non-related function piece
    //do something with the arguments that were passed
    $('span', '#last').text(id);
});

$('.click').click(function() {
    var $ele = $(this).next('div').find('[id^="clickCount"]');
    clickCallbacks.fireWith($ele, [this.id]); //do two separate but related things.
});

  • 现在可以使用的是多个回调批处理函数,您可以在需要时立即调用这些函数,而无需在整个代码中进行大量更改.
  • 这篇关于什么时候使用JQuery.Callbacks?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆