Quantcast
Channel: jQuery Library for SharePoint Web Services
Viewing all articles
Browse latest Browse all 6517

New Post: Trying to use SPServices Promise with AngularJS Promise

$
0
0
I've written a small SPA application with SPServices, but decided to rewrite most of it to use Promises and improve things in general. I've decided to write this application using AngularJS, which has it's own implementation of Promises and I'm not sure if this conflicts with SPServices.

I tried to write my function based on your Promises blog post

This is my function:
  var addUserToGroup = function (selectedUser, selectedAvailableGroups, assignedGroups, availableGroups) {
    var addPromise = [];
    var selectLength = selectedAvailableGroups.length;
    var deferred = $q.defer();

    //Add user to selected groups on server
    for (var i = 0; i < selectLength; i++) {
      addPromise[i] = $().SPServices({
        operation: "AddUserToGroup",
        groupName: selectedAvailableGroups[i].name,
        userLoginName: selectedUser.domain
      });      
    };

    //when all users added, update dom
    $.when.apply($,addPromise).done(function (){
      for (var i = 0; i < selectLength; i++) {
        assignedGroups.push(selectedAvailableGroups[i]);
        //availableGroups.pop(selectedAvailableGroups[i]);
      };
      console.log(selectedUser.name + " added to: " + JSON.stringify(selectedAvailableGroups));  
      return deferred.promise;
    });
  }
However, since most of this code didn't use Angular syntax, the DOM didn't update correctly. I was told on stack overflow that I needed to use Angular Promises which calls Angular's $apply() function, necessary to update dependencies. So I've gone from not using any promises, to having to use 2 from different frameworks.

Is there a way to not have to use $.when and using Angular promises only? Right now the SPSerivice makes the group change but I need to app to respond to this change.

Viewing all articles
Browse latest Browse all 6517

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>