我试图从示例应用程序中找出主干(请参阅https://github.com/elfsternberg/The-Backbone-Store)。代码使用jQuery的Deferred和promise(),如您在下面的代码中看到的。我已经阅读了关于jQuery的文档,但从下面的示例中找出如何使用这些方法时遇到困难。你可能需要更多的代码来回答这个问题,但也许不需要。这些是我有关于它的问题jQuery:推迟/承诺
1)是否dfd.resolve调用一次fadeOut完成?如果是这样,dfd.resolve触发器是什么?
2)返回的信息promise.promise();它是否调用延迟方法?什么时候?为什么这样做?这似乎是一个递归方法?
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();
}
对于第二个问题,看http://stackoverflow.com/q/8073528/1048572 – Bergi