理解 $.Deferred() 序列和 $.fn.pipe()
我陷入了这段代码:
reload: function() {
var self = this;
$.when(self.unload()).then(function() {
self.load();
});
},
load: function() {
var self = this;
$.ajax({
url: self.options.url,
success: function(data) {
$.Deferred(
function(dfr) {
$.each(data, function() {
var div = $('<div></div>')
.addClass('nw-item')
.html('newsitem.ejs', this)
.hide()
.appendTo(self.element);
dfr = dfr.pipe(function() {
return div.slideDown(self.options.speed);
});
});
}).resolve();
}
})
},
unload: function() {
var items = this.element.children('.nw-item').reverse(),
self = this,
udfd = $.Deferred(
function(dfr) {
$.each(items, function() {
var div = $(this);
dfr = dfr.pipe(function() {
return div.slideUp(self.options.speed);
});
});
}).resolve();
return udfd;
}
在“重新加载”中,我需要在“卸载”序列完成后启动“加载”序列,但它在“卸载”序列元素的第一次迭代被隐藏后立即启动。请帮助我,我被困住了(
I am stuck in this code:
reload: function() {
var self = this;
$.when(self.unload()).then(function() {
self.load();
});
},
load: function() {
var self = this;
$.ajax({
url: self.options.url,
success: function(data) {
$.Deferred(
function(dfr) {
$.each(data, function() {
var div = $('<div></div>')
.addClass('nw-item')
.html('newsitem.ejs', this)
.hide()
.appendTo(self.element);
dfr = dfr.pipe(function() {
return div.slideDown(self.options.speed);
});
});
}).resolve();
}
})
},
unload: function() {
var items = this.element.children('.nw-item').reverse(),
self = this,
udfd = $.Deferred(
function(dfr) {
$.each(items, function() {
var div = $(this);
dfr = dfr.pipe(function() {
return div.slideUp(self.options.speed);
});
});
}).resolve();
return udfd;
}
in "reload" I need to start "load" sequence after "unload" sequence is completed, but it starts right after first iteration of "unload" sequence element becomes hidden. Help me please, I'm stuck(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未经测试,但尝试以下操作:
由于您的 jQuery.slideUp 函数是异步的,但不会返回延迟对象,例如 jQuery.ajax,因此您必须手动返回延迟/承诺。当你的管道函数链完成后,它会解析主要的 Deferred。不要忘记返回主要 Deferred 的承诺。
就像我说的,不能保证,但你可以尝试一下!
Not tested but try this:
Since your jQuery.slideUp function is asynchronous BUT does not return an Deferred Object like e.g. jQuery.ajax you have to manually return a Deferred/promise. When your chain of piped functions is done it resolves the main Deferred. Don't forget to return a promise for the main Deferred.
Like I said, no guarantee but you could give it a try!