Skip to content

Commit

Permalink
Merge pull request #349 from marmelab/reference_failed
Browse files Browse the repository at this point in the history
[RFR] Allow to display the listView even with missing references
  • Loading branch information
manuquentin committed Mar 31, 2015
2 parents 54cefb5 + 30223f1 commit 6c4cc99
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 184 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 @@ -25,6 +25,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
56 changes: 56 additions & 0 deletions src/javascripts/ng-admin/Crud/misc/PromisesResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*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
for (var i in states) {
if (!states[i]) {
return;
}
}
deferred.resolve(results);
}
function resolveSuccess(result) {
return resolve({ status: 'success', result: result });
}
function resolveError(result) {
return resolve({ status: 'error', error: result })
}
// whether the promise ends with success or error, consider it done
$q.when(promise).then(resolveSuccess, resolveError);
});

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;
});
23 changes: 19 additions & 4 deletions src/javascripts/ng-admin/Crud/repository/RetrieveQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,37 @@ define(function (require) {
}

// Fill all reference entries
return this.$q.all(calls)
return this.PromisesResolver.allEvenFailed(calls)
.then(function (responses) {
if (responses.length === 0) {
return references;
}
i = 0;
var response;

for (j in references) {

reference = references[j];
singleCallFilters = reference.getSingleApiCall(identifiers);

// Retrieve entries depending on 1 or many request was done
if (singleCallFilters || !rawValues) {
references[j].entries = reference.getReferencedView().mapEntries(responses[i++].data);
response = responses[i++];
if (response.status == 'error') {
// the response failed
continue;
}
references[j].entries = reference.getReferencedView().mapEntries(response.result.data);
} else {
entries = [];
identifiers = reference.getIdentifierValues(rawValues);
for (k in identifiers) {
entries.push(responses[i++]);
response = responses[i++];
if (response.status == 'error') {
// one of the responses failed
continue;
}
entries.push(response.result);
}

// Entry are already mapped by getOne
Expand Down Expand Up @@ -287,7 +302,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 @@ -36,7 +36,7 @@ define(function (require) {
});
};

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

return UpdateQueries;
});
1 change: 1 addition & 0 deletions src/javascripts/test/app-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ requirejs.config({
'angular-mocks': 'bower_components/angular-mocks/angular-mocks',
'mixins': '/base/test/mock/mixins',
'mock/q': '/base/test/mock/q',
'mock/PromisesResolver': '/base/test/mock/PromisesResolver',
'mock/Restangular': '/base/test/mock/Restangular',

'angular': 'bower_components/angular/angular',
Expand Down
9 changes: 9 additions & 0 deletions src/javascripts/test/mock/PromisesResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*global jasmine,define*/

define('mock/PromisesResolver', ['mixins'], function (mixins) {
"use strict";

return {
allEvenFailed: function() { return mixins.buildPromise([]); }
};
});
Loading

0 comments on commit 6c4cc99

Please sign in to comment.