如何jQuery的承诺方法真的管用吗? [英] how does jquery's promise method really work?

查看:149
本文介绍了如何jQuery的承诺方法真的管用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白这个代理,并承诺事。根据文档,委托将一个选择器和事件绑定某种包装容器,可再次使用在以后的时间为当前和未来的项目?

I don't really understand this delegate and promise thing. According to the docs, delegate would bind an selector and event to some sort of wrapping container that can be used again at a later time for current and future items?

诺()将重新映射回事情时,如果一切新加载的比赛是第一界定?也许我真的不明白这个承诺的方法。

promise() would remap things back to when it was first bounded if everything newly loaded matches? Maybe I don't really understand this promise method.

如果包装仍然存在,但在包装容器中的内容发生了变化,或者通过ajax重载?为什么打破该事件不会触发或者,因为它会在第一时间就被绑定工作?

What if the wrapper is still there, but the contents in the wrapper container has changed, and or reloaded via ajax? Why is it broken that the events are not triggering or working as it would the first time it is binded?

是的,我已经到了文档页面,我只是不明白,他们的解释完全

And yes, I have been to the docs page, I just don't understand their explanations completely

推荐答案

我有点被这个问题困惑。我想这是因为的的是由困惑承诺代理。它们实际上是完全的jQuery无关的功能。我将分别解释每个:

I'm a bit confused by this question. I think this is because you are confused by promise and delegate. They are in fact completely unrelated features of jQuery. I'll explain each separately:

代理 是jQuery的那是在jQuery的1.4中引入的功能0.2。 (这是一个更好的办法来 生活 功能的加入jQuery中1.3)。它解决了附带修改DOM,尤其是使用AJAX调用一个特别的问题。

delegate is a feature of jQuery that was introduced in jQuery 1.4.2. (It is a nicer approach to the live feature that was added in jQuery 1.3). It solves a particular problem that comes with modifying the DOM, and particularly with AJAX calls.

当您绑定一个事件处理程序,将其绑定到一个选择。所以,你可能会做 $('特殊')。点击(FN)将事件处理程序绑定到所有成员的特别类。绑定到这些因素,因此,如果您再从这些元素中的一个取出类,事件仍然会触发。相反地​​,如果将类添加到一个元件(或添加新元素到DOM),它将不具有事件的约束。

When you bind an event handler, you bind it to a selection. So you might do $('.special').click(fn) to bind an event handler to all the members of the special class. You bind to those elements, so if you then remove the class from one of those elements, the event will still be triggered. Inversely, if you add the class to an element (or add a new element into the DOM), it won't have the event bound.

目前的Javascript的功能,缓解这一被称为事件冒泡。当事件被触发,首先,浏览器会通知该事件起源的元素。然后,它上升的DOM树,并通知每个祖先元素。这意味着你可以绑定一个事件处理程序的元素上高高的DOM树,以及事件触发的任何子元素(即使是那些在处理程序被捆绑不存在的)。

There is a feature of Javascript that mitigates this called "event bubbling". When an event is triggered, first the browser notifies the element where the event originated. Then it goes up the DOM tree, and notifies each ancestor element. This means that you can bind an event handler on an element high up the DOM tree, and events triggered on any child elements (even those that don't exist when the handler was bound).

代理是jQuery的实现了这一点。首先,选择一个父元素。然后你指定一个选择&ndash的;如果原始元素这个选择相匹配的处理器将只运行。然后你指定的事件类型,如点击提交的keydown ,就像 绑定 。然后最后你指定的事件处理程序。

delegate is jQuery's implementation of this. First, you select a parent element. Then you specify a selector – the handler will only be run if the originating element matches this selector. Then you specify an event type, such as click, submit, keydown, just as with bind. Then finally you specify the event handler.

$('#containingElement').delegate('a.special', 'click', function() {
    alert('This will happen on all links with the special class');
});

是另一种相对较新的除了jQuery的功能集。这是 递延的一部分 的概念被引入了jQuery的1.5。 (我认为的相似性延迟和代表很可能是混乱的根源之间的声音。)这是抽象掉异步code并发症的一种方式。这方面最好的例子是AJAX调用,作为对象返回 $。阿贾克斯延迟对象。例如:

promise

promise is another relatively recent addition to the jQuery featureset. It is part of the Deferred concept that was introduced in jQuery 1.5. (I think the similarity in sound between "deferred" and "delegate" is probably the source of confusion.) This is a way of abstracting away the complications of asynchronous code. The best example of this is with AJAX calls, as the object returned by $.ajax is a Deferred object. For instance:

$.ajax({
    url: 'somepage.cgi',
    data: {foo: 'bar'}
}).done(function() {
    // this will be run when the AJAX request succeeds
}).fail(function() {
    // this will be run when the AJAX request fails
}).always(function() {
    // this will be run when the AJAX request is complete, whether it fails or succeeds
}).done(function() {
    // this will also be run when the AJAX request succeeds
});

因此​​,在许多方面是相同的绑定成功处理程序中的 $。阿贾克斯通话,但您可以绑定多个处理程序,并可以将它们绑定在最初的呼叫。

So it is in many ways the same as binding success handlers in the $.ajax call, except that you can bind more than one handler, and you can bind them after the initial call.

当它是有用的异步处理还有一次是动画。您可以提供回调的功能,但它会更好,以类似的语法我上面提供的AJAX例子做到这一点。

Another time when it would be useful to deal asynchronously is with animations. You can provide callbacks to functions, but it would be nicer to do this with similar syntax to the AJAX example I've provided above.

在jQuery的1.6,这个功能成为可能,并 是该实现的一部分。你叫在一个jQuery选择,你会得到你能事件处理程序绑定到一个对象,当对象中的所有动画完成。

In jQuery 1.6, this functionality was made possible, and promise is part of this implementation. You call promise on a jQuery selection, and you'll get an object that you can bind event handlers to, when all the animations in the object have completed.

例如:

$('div.special').fadeIn(5000).promise().then(function() {
    // run when the animation succeeds
}).then(function() {
    // also run when the animation succeeds
});

再次,这是在效果与传统方法类似,但它增加了灵活性。以后,您可以绑定处理程序,并可以绑定多个。

Again, this is similar in effect to traditional methods, but it adds flexibility. You can bind the handlers later, and you can bind more than one.

基本上,之间不存在显著关系代理,但他们无论是在现代的实用功能jQuery的。

Basically, there is no significant relationship between delegate and promise, but they're both useful features in modern jQuery.

这篇关于如何jQuery的承诺方法真的管用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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