了解jQuery Deferred.pipe() [英] Understanding jQuery Deferred.pipe()
本文介绍了了解jQuery Deferred.pipe()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在以下情况下实现jQuery Deferred.pipe()方法:
I am trying to implement the jQuery Deferred.pipe() method for the following scenario:
- 通过$ .ajax()在数据库中添加用户
- 获取响应,无论是否正确添加了用户.
- 如果成功添加,请通过$ .ajax()从服务器获取所有用户列表
- 通过jQuery模板显示列表
这是我尝试过的事情:
var addUserSuccess = function( data ) {
if ( data.returnCode !== "success" ) {
return $.Deferred().reject('Error saving user');
}
getUsers();
}
var addUser = function() {
return $.ajax(url, {
type: "POST",
data: { username: 'test' },
contentType: "application/json",
dataType: "json"
});
}
var displayUsers = function( data ) {
$('#myTmpl').tmpl(data.d).appendTo('#myDiv');
}
var getUsers = function () {
return $.ajax(url, {
type: "GET",
data: {},
contentType: "application/json",
dataType: "json"
});
}
$.when(addUser()).pipe(addUserSuccess).then(displayUsers)
但这不起作用.我知道有些东西遗失或错误.有帮助吗?
But this does not work. I know there is something missing or wrong. Any help?
推荐答案
在addUsersSuccess中,您应该返回getUsers.这是一个简单的错字,您完全正确地掌握了主要思想,并按原样使用管道,这很不错:)
In addUsersSuccess, you should be returning getUsers. It's a simple typo, you got the main idea perfectly right and are using pipe as it should be and nicely :)
这篇关于了解jQuery Deferred.pipe()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文