Skip to content

Commit

Permalink
Move specific promise resolver to a standalone service
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Mar 10, 2015
1 parent 78b103e commit b0afec0
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 63 deletions.
1 change: 1 addition & 0 deletions src/javascripts/ng-admin/Crud/CrudModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
53 changes: 53 additions & 0 deletions src/javascripts/ng-admin/Crud/misc/PromisesResolver.js
Original file line number Diff line number Diff line change
@@ -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;
});
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/repository/CreateQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ define(function (require) {
});
};

CreateQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration'];
CreateQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration', 'PromisesResolver'];

return CreateQueries;
});
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/repository/DeleteQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ define(function (require) {
.customDELETE();
};

DeleteQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration'];
DeleteQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration', 'PromisesResolver'];

return DeleteQueries;
});
5 changes: 3 additions & 2 deletions src/javascripts/ng-admin/Crud/repository/Queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
4 changes: 2 additions & 2 deletions src/javascripts/ng-admin/Crud/repository/RetrieveQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -298,7 +298,7 @@ define(function (require) {
return entry;
};

RetrieveQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration'];
RetrieveQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration', 'PromisesResolver'];

return RetrieveQueries;
});
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/repository/UpdateQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ define(function (require) {
});
};

UpdateQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration'];
UpdateQueries.$inject = ['$q', 'Restangular', 'NgAdminConfiguration', 'PromisesResolver'];

return UpdateQueries;
});
1 change: 0 additions & 1 deletion src/javascripts/ng-admin/Main/MainModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
55 changes: 0 additions & 55 deletions src/javascripts/ng-admin/Main/config/q.js

This file was deleted.

0 comments on commit b0afec0

Please sign in to comment.