diff --git a/src/javascripts/ng-admin/Crud/CrudModule.js b/src/javascripts/ng-admin/Crud/CrudModule.js index 5096b5b0..205251cc 100644 --- a/src/javascripts/ng-admin/Crud/CrudModule.js +++ b/src/javascripts/ng-admin/Crud/CrudModule.js @@ -23,6 +23,7 @@ define(function (require) { CrudModule.controller('FormController', require('ng-admin/Crud/form/FormController')); CrudModule.controller('DeleteController', require('ng-admin/Crud/delete/DeleteController')); + CrudModule.service('PromisesResolver', require('ng-admin/Crud/misc/PromisesResolver')); CrudModule.service('RetrieveQueries', require('ng-admin/Crud/repository/RetrieveQueries')); CrudModule.service('CreateQueries', require('ng-admin/Crud/repository/CreateQueries')); CrudModule.service('UpdateQueries', require('ng-admin/Crud/repository/UpdateQueries')); diff --git a/src/javascripts/ng-admin/Crud/misc/PromisesResolver.js b/src/javascripts/ng-admin/Crud/misc/PromisesResolver.js new file mode 100644 index 00000000..f7407088 --- /dev/null +++ b/src/javascripts/ng-admin/Crud/misc/PromisesResolver.js @@ -0,0 +1,53 @@ +/*global define*/ +define(function () { + 'use strict'; + + function PromisesResolver($q) { + + function allEvenFailed(promises) { + if (!Array.isArray(promises)) { + throw 'allEvenFailed can only handle an array of promises'; + } + var deferred = $q.defer(); + if (promises.length === 0) { + deferred.resolve([]); + return deferred.promise; + } + + var states = []; + var results = []; + + promises.forEach(function (promise, key) { + states[key] = false; // promises are not resolved by default + }); + + promises.forEach(function (promise, key) { + function resolve(result) { + states[key] = true; + results[key] = result; // result may be an error + var allFinished = true; + states.forEach(function (state) { + if (!state) { + allFinished = false; + } + }); + if (allFinished) { + deferred.resolve(results); + } + } + // whether the promise ends with success or error, consider it done + $q.when(promise).then(resolve, resolve); + }); + + return deferred.promise; + }; + + return { + allEvenFailed: allEvenFailed + }; + } + + PromisesResolver.$inject = ['$q']; + + return PromisesResolver; +}); diff --git a/src/javascripts/ng-admin/Crud/repository/CreateQueries.js b/src/javascripts/ng-admin/Crud/repository/CreateQueries.js index 13ff43cc..dcb78a91 100644 --- a/src/javascripts/ng-admin/Crud/repository/CreateQueries.js +++ b/src/javascripts/ng-admin/Crud/repository/CreateQueries.js @@ -32,7 +32,7 @@ define(function (require) { }); }; - CreateQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration']; + CreateQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration', 'PromisesResolver']; return CreateQueries; }); diff --git a/src/javascripts/ng-admin/Crud/repository/DeleteQueries.js b/src/javascripts/ng-admin/Crud/repository/DeleteQueries.js index 1e1eddb1..ca3f34b3 100644 --- a/src/javascripts/ng-admin/Crud/repository/DeleteQueries.js +++ b/src/javascripts/ng-admin/Crud/repository/DeleteQueries.js @@ -29,7 +29,7 @@ define(function (require) { .customDELETE(); }; - DeleteQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration']; + DeleteQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration', 'PromisesResolver']; return DeleteQueries; }); diff --git a/src/javascripts/ng-admin/Crud/repository/Queries.js b/src/javascripts/ng-admin/Crud/repository/Queries.js index 359a209c..786b0bbf 100644 --- a/src/javascripts/ng-admin/Crud/repository/Queries.js +++ b/src/javascripts/ng-admin/Crud/repository/Queries.js @@ -10,15 +10,16 @@ define(function () { * @param {Application} Configuration * @constructor */ - function Queries($q, Restangular, Configuration) { + function Queries($q, Restangular, Configuration, PromisesResolver) { this.$q = $q; this.Restangular = Restangular; this.config = Configuration(); + this.PromisesResolver = PromisesResolver; this.Restangular.setFullResponse(true); // To get also the headers } - Queries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration']; + Queries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration', 'PromisesResolver']; return Queries; }); diff --git a/src/javascripts/ng-admin/Crud/repository/RetrieveQueries.js b/src/javascripts/ng-admin/Crud/repository/RetrieveQueries.js index 4798b859..fc3f8f77 100644 --- a/src/javascripts/ng-admin/Crud/repository/RetrieveQueries.js +++ b/src/javascripts/ng-admin/Crud/repository/RetrieveQueries.js @@ -159,7 +159,7 @@ define(function (require) { } // Fill all reference entries - return this.$q.allEvenFailed(calls) + return this.PromisesResolver.allEvenFailed(calls) .then(function (responses) { i = 0; var response; @@ -298,7 +298,7 @@ define(function (require) { return entry; }; - RetrieveQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration']; + RetrieveQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration', 'PromisesResolver']; return RetrieveQueries; }); diff --git a/src/javascripts/ng-admin/Crud/repository/UpdateQueries.js b/src/javascripts/ng-admin/Crud/repository/UpdateQueries.js index 0fe29dd3..ef1630ed 100644 --- a/src/javascripts/ng-admin/Crud/repository/UpdateQueries.js +++ b/src/javascripts/ng-admin/Crud/repository/UpdateQueries.js @@ -35,7 +35,7 @@ define(function (require) { }); }; - UpdateQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration']; + UpdateQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration', 'PromisesResolver']; return UpdateQueries; }); diff --git a/src/javascripts/ng-admin/Main/MainModule.js b/src/javascripts/ng-admin/Main/MainModule.js index 9b30cfb4..bd08708b 100644 --- a/src/javascripts/ng-admin/Main/MainModule.js +++ b/src/javascripts/ng-admin/Main/MainModule.js @@ -39,7 +39,6 @@ define(function (require) { MainModule.directive('menu', require('ng-admin/Main/component/directive/Menu')); MainModule.config(require('ng-admin/Main/config/http')); - MainModule.config(require('ng-admin/Main/config/q')); MainModule.config(require('ng-admin/Main/config/routing')); MainModule.config(require('ng-admin/Main/config/factories')); diff --git a/src/javascripts/ng-admin/Main/config/q.js b/src/javascripts/ng-admin/Main/config/q.js deleted file mode 100644 index d921842a..00000000 --- a/src/javascripts/ng-admin/Main/config/q.js +++ /dev/null @@ -1,55 +0,0 @@ -/*global define*/ - -define(function () { - 'use strict'; - - var angular = require('angular') - - function qDecorator($provide) { - // taken from http://stackoverflow.com/a/18891109/1333479 - $provide.decorator('$q', ['$delegate', function ($delegate) { - var $q = $delegate; - - $q.allEvenFailed = function (promises) { - if (!angular.isArray(promises)) { - throw 'allSettled can only handle an array of promises (for now)'; - } - var deferred = $q.defer(); - var states = []; - var results = []; - - // First create an array for all promises with their state - angular.forEach(promises, function (promise, key) { - states[key] = false; - }); - - // Loop through the promises - // a second loop to be sure that checkStates is called when all states are set to false first - angular.forEach(promises, function (promise, key) { - function resolve(result) { - states[key] = true; - results[key] = result; - var allFinished = true; - angular.forEach(states, function (state) { - if (!state) { - allFinished = false; - } - }); - if (allFinished) { - deferred.resolve(results); - } - } - $q.when(promise).then(resolve, resolve); - }); - - return deferred.promise; - }; - - return $q; - }]); - } - - qDecorator.inject = ['$provide']; - - return qDecorator; -});