阿贾克斯承诺不起作用
Ajax promise not working
我正在尝试使用 promise 返回当前登录用户和 SharePoint 列表中字段的比较。
function compareCurrentUserWithListObject() {
var userProp = this._userProfileProperties;
var userName = userProp.get_userProfileProperties()['UserName'];
return this._list.filter(function (element, index, array) {
var promise = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
type: "GET",
headers: { "Accept": "application/json;odata=verbose" }
});
promise.done(function(data) {
return (data.d.Email.indexOf(userName) > -1);
});
});
}
function init() {
var userArray = this.compareCurrentUserWithListObject();
userArray.done(function(res) {
if (res.length > 0) {
//Do some stuff after compare...
}
});
}
我不确定我在这里使用的 .done 是否正确。有人可以帮助我吗?
编辑:
工作代码:
function compareCurrentUserWithListObject() {
var userProp = this._userProfileProperties;
var userName = userProp.get_userProfileProperties()['UserName'];
return this._list.filter(function (element, index, array) {
var promise = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
type: "GET",
headers: { "Accept": "application/json;odata=verbose" }
});
promise.done(function(data) {
return (data.d.Email.indexOf(userName) > -1);
});
return promise;
});
}
function init() {
var userArray = this.compareCurrentUserWithListObject();
if (userArray.length > 0) {
//Do some stuff after compare...
}
}
你需要返回承诺
function compareCurrentUserWithListObject() {
var userProp = this._userProfileProperties;
var userName = userProp.get_userProfileProperties()['UserName'];
return this._list.filter(function (element, index, array) {
var promise = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
type: "GET",
headers: { "Accept": "application/json;odata=verbose" }
});
promise.done(function(data) {
return (data.d.Email.indexOf(userName) > -1);
});
// return promise here
return promise;
});
}
或者这个(IMO更干净):
function compareCurrentUserWithListObject() {
var userProp = this._userProfileProperties;
var userName = userProp.get_userProfileProperties()['UserName'];
return jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetUserById(" + element.user.get_lookupId() + ")",
type: "GET",
headers: { "Accept": "application/json;odata=verbose" }
});
}
function init() {
this.compareCurrentUserWithListObject()
.done(function(data) {
var res = data.d.Email.indexOf(userName) > -1;
if (res.length > 0) {
//Do some stuff after compare...
}
});
}
看起来您想在 init
中使用响应之前对其进行修改。有一种方法可以做到这一点,但我会在使用它时在.done
回调中执行此操作。
我没有测试此代码,因此可能存在错误。但一般的答案是:你需要回报承诺。
使用承诺执行此操作的惯用方法是使用 Promise.all()
。(我将以 Q promise 为例,但 Promise.all 内置于 JS6 promise API 和其他几个 promise 库中):
function getUserInfo(listItem) {
var promise = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl +
"/_api/web/GetUserById(" + listItem.user.get_lookupId() + ")",
type: "GET",
headers: { "Accept": "application/json;odata=verbose" }
});
return Q.Promise.when(promise);
}
function filterUsers(users) {
var userProp = this._userProfileProperties;
var userName = userProp.get_userProfileProperties()['UserName'];
return users.filter(function (user) {
return user.d.Email.indexOf(userName) > -1;
});
}
function init() {
Q.Promise.all(this._list.map(getUserInfo))
.then(filterUsers.bind(this))
.then(function (matchedUsers) {
// matchedUsers is an array of all the users you want.
// use it as needed
});
}
相关文章:
- $http中的Angular 1数据绑定承诺不起作用
- 手动拒绝路由承诺;不起作用
- 角度控制器服务承诺不起作用
- Deftjs承诺done()不起作用
- OfflineAudioContext.startRendering()基于承诺的函数在safari中不起作用
- 角度承诺不起作用
- jQuery ajax 请求与承诺在IE9中不起作用
- 阿贾克斯承诺不起作用
- 角度承诺 ($q) 不起作用
- 为什么角度承诺跟踪器对我不起作用
- 为什么在Lightswitch中堆叠承诺的一种方法有效,而另一种方法则不起作用
- 承诺链示例不起作用
- 为什么这个承诺不起作用
- Ember数据's商店承诺;t在五次测试中不起作用
- 链式Q承诺序列不起作用
- 承诺链不起作用.为什么
- Q承诺在lodash约简函数中不起作用
- 带有嵌套AJAX调用的JavaScript承诺不起作用
- 为什么回流.js听和承诺助手不起作用
- Javascript承诺在我的情况下不起作用