weixin_33720956 2015-01-02 12:47 采纳率: 0%
浏览 24

Ajax承诺不起作用

I'm trying to use promise to return a comparison of current logged in user and a field from a list in 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...
     }
   });
}

I'm not sure I'm using the .done correct here. Can someone help me?

EDIT:

Working code:

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...
     }
}
  • 写回答

2条回答 默认 最新

  • YaoRaoLov 2015-01-02 12:54
    关注

    you need to return the promise

    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;
       });
    }   
    

    or this (which is cleaner 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...
         }
       });
    
    }
    

    it looks like you want to modify the response before using it in init. There is a way to do that but I'd do it inside the .done callback when using it.

    I didn't test this code so there might be mistakes. But the general answer is: you need to return the promise.

    评论

报告相同问题?