简体   繁体   English

jQuery:延迟/承诺

[英]jQuery: deferred/ promise

I'm trying to figure out backbone from an example app (see https://github.com/elfsternberg/The-Backbone-Store ). 我正在尝试从示例应用程序中找出主干(请参阅https://github.com/elfsternberg/The-Backbone-Store )。 The code uses jQuery's Deferred and promise(), as you see in the code below. 如下面的代码所示,该代码使用jQuery的Deferred和promise()。 I've read the docs on jQuery, but am having trouble figuring out from the example below how these methods are used. 我已经阅读了jQuery上的文档,但是无法从下面的示例中弄清楚如何使用这些方法。 You might need more code to answer this question, but maybe not. 您可能需要更多代码来回答这个问题,但是可能不需要。 These are the questions I have about it 这些是我对此的疑问

1) is dfd.resolve called once fadeOut is done? 1)淡出完成后是否调用dfd.resolve? if so, what does dfd.resolve trigger? 如果是这样,dfd.resolve会触发什么?

2) What is happening by returning promise.promise(); 2)通过返回promise.promise()发生了什么事; is it calling the Deferred method? 它在调用Deferred方法吗? when? 什么时候? why is it done this way? 为什么这样做呢? this seems like a recursive method? 这似乎是一种递归方法?

3) is it possible that dfd.resolve is triggering other methods not shown in this code? 3)dfd.resolve是否有可能触发此代码中未显示的其他方法?

      hide: function() {
            if ((":visible") === false) {

                return null;

            }
            promise = $.Deferred(_.bind(function(dfd) { 
                this.el.fadeOut('fast', dfd.resolve)}, this));
            return promise.promise();
        },

        show: function() {
            if (this.el.is(':visible')) { 
                return;
            }       
            promise = $.Deferred(_.bind(function(dfd) { 
                console.log("in promise section of show in base view");
                this.el.fadeIn('fast', dfd.resolve) }, this))
            return promise.promise();
        }

1) is dfd.resolve called once fadeOut is done? 1)淡出完成后是否调用dfd.resolve? if so, what does dfd.resolve trigger? 如果是这样,dfd.resolve会触发什么?

Yes. 是。 jQuery.fadeOut takes in a callback as one of it's parameters. jQuery.fadeOut将回调作为参数之一。 Once the animation is complete it will execute the callback. 动画完成后,它将执行回调。 In this case, it happens to be the resolve method of the Deferred. 在这种情况下,它恰好是Deferred的resolve方法。

2) What is happening by returning promise.promise(); 2)通过返回promise.promise()发生了什么事; is it calling the Deferred method? 它在调用Deferred方法吗? when? 什么时候? why is it done this way? 为什么这样做呢? this seems like a recursive method? 这似乎是一种递归方法?

Nothing recursive is going on here . 这里没有递归操作 promise is just a variable that holds a reference to the created Deferred object. promise只是一个变量,其中包含对创建的Deferred对象的引用。 promise() is a method on a jQuery.Deferred that returns a modified version of the Deferred that does not allow you to modify how it behaves. promise()jQuery.Deferred上的一种方法,该方法返回Deferred的修改版本,该版本不允许您修改其行为。 Hence the promise that the caller can be sure it will always execute the same way. 因此,保证调用者可以确保它将始终以相同的方式执行。

3) is it possible that dfd.resolve is triggering other methods not shown in this code? 3)dfd.resolve是否有可能触发此代码中未显示的其他方法?

Yes. 是。 A Deferred is nothing more than an object that allows you to register callbacks. Deferred仅仅是允许您注册回调的对象。 Calling .resolve() on a Deferred will trigger the done handlers , while calling .reject() will trigger any fail handlers . 在Deferred上调用.resolve()将触发完成的处理程序 ,而在调用.reject()将触发任何失败的处理程序

A very shorthand example might look like this: 一个非常速记的示例可能如下所示:

//A Deferred takes in a function that will be passed a reference
// to the Deferred object. This allows you to resolve or reject
// it at some point in the future.
var promise = $.Deferred(function(def){

   setTimeout(function(){

      def.resolve('Five seconds have passed!');      

   }, 5000);

}).promise();

//This will only get executed when the
// underlying Deferred gets resolved
promise.done(function(msg){

   alert(msg); // Displays: 'Five seconds have passed!'

});

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

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