Changes the user controller saving extensions operation to use events instead

This commit is contained in:
Shannon
2018-01-05 10:22:46 +11:00
parent 0310782d7d
commit 8d4958e395
2 changed files with 27 additions and 58 deletions

View File

@@ -16,52 +16,16 @@ function eventsService($q, $rootScope) {
return {
/** raise an event with a given name, returns an array of promises for each listener */
emit: function (name, args) {
/** raise an event with a given name */
emit: function (name, args) {
//there are no listeners
if (!$rootScope.$$listeners[name]) {
return;
//return [];
}
//send the event
$rootScope.$emit(name, args);
//PP: I've commented out the below, since we currently dont
// expose the eventsService as a documented api
// and think we need to figure out our usecases for this
// since the below modifies the return value of the then on() method
/*
//setup a deferred promise for each listener
var deferred = [];
for (var i = 0; i < $rootScope.$$listeners[name].length; i++) {
deferred.push($q.defer());
}*/
//create a new event args object to pass to the
// $emit containing methods that will allow listeners
// to return data in an async if required
/*
var eventArgs = {
args: args,
reject: function (a) {
deferred.pop().reject(a);
},
resolve: function (a) {
deferred.pop().resolve(a);
}
};*/
/*
//return an array of promises
var promises = _.map(deferred, function(p) {
return p.promise;
});
return promises;*/
},
/** subscribe to a method, or use scope.$on = same thing */

View File

@@ -1,7 +1,7 @@
(function () {
"use strict";
function UserEditController($scope, $q, $timeout, $location, $routeParams, formHelper, usersResource, userService, contentEditingHelper, localizationService, notificationsService, mediaHelper, Upload, umbRequestHelper, usersHelper, authResource, dateHelper) {
function UserEditController($scope, eventsService, $q, $timeout, $location, $routeParams, formHelper, usersResource, userService, contentEditingHelper, localizationService, notificationsService, mediaHelper, Upload, umbRequestHelper, usersHelper, authResource, dateHelper) {
var vm = this;
@@ -137,7 +137,7 @@
.then(function (saved) {
//if the user saved, then try to execute all extended save options
extendedSave().then(function(result) {
extendedSave(saved).then(function(result) {
//if all is good, then reset the form
formHelper.resetForm({ scope: $scope, notifications: saved.notifications });
}, function(err) {
@@ -171,26 +171,31 @@
});
}
}
function extendedSave() {
//create a promise for each save method
var savePromises = {};
angular.forEach(vm.extendedSaveMethods, function (val, key) {
var deferred = $q.defer();
savePromises[key] = deferred;
});
var allPromises = _.map(savePromises, function (p) {
return p.promise;
})
/**
* Used to emit the save event and await any async operations being performed by editor extensions
* @param {any} savedUser
*/
function extendedSave(savedUser) {
//used to track any promises added by the event handlers to be awaited
var promises = [];
var args = {
//getPromise: getPromise,
user: savedUser,
//a promise can be added by the event handler if the handler needs an async operation to be awaited
addPromise: function (p) {
promises.push(p);
}
};
//emit the event
eventsService.emit("editors.user.editController.save", args);
//await all promises to complete
var resultPromise = $q.all(allPromises);
//execute all promises by passing them to the save methods
angular.forEach(vm.extendedSaveMethods, function (func, key) {
func(savePromises[key]);
});
var resultPromise = $q.all(promises);
return resultPromise;
}