[Persistence] Load models from multiple spaces

This commit is contained in:
Victor Woeltjen 2015-09-02 11:16:19 -07:00
parent 8759fdbd95
commit ba6e542d08
2 changed files with 38 additions and 8 deletions

View File

@ -63,7 +63,12 @@
"provides": "modelService",
"type": "provider",
"implementation": "models/PersistedModelProvider.js",
"depends": [ "persistenceService", "$q", "PERSISTENCE_SPACE" ]
"depends": [
"persistenceService",
"$q",
"PERSISTENCE_SPACE",
"ADDITIONAL_PERSISTENCE_SPACES"
]
},
{
"provides": "modelService",
@ -215,6 +220,17 @@
"composition": []
}
}
],
"constants": [
{
"key": "PERSISTENCE_SPACE",
"value": "mct"
},
{
"key": "ADDITIONAL_PERSISTENCE_SPACES",
"value": [],
"description": "An array of additional persistence spaces to load models from."
}
]
}
}

View File

@ -39,23 +39,37 @@ define(
* @param {PersistenceService} persistenceService the service in which
* domain object models are persisted.
* @param $q Angular's $q service, for working with promises
* @param {string} SPACE the name of the persistence space from which
* models should be retrieved.
* @param {string} space the name of the persistence space(s)
* from which models should be retrieved.
* @param {string} spaces additional persistence spaces to use
*/
function PersistedModelProvider(persistenceService, $q, space) {
function PersistedModelProvider(persistenceService, $q, space, spaces) {
this.persistenceService = persistenceService;
this.$q = $q;
this.space = space;
this.spaces = [space].concat(spaces || []);
}
// Take the most recently modified model, for cases where
// multiple persistence spaces return models.
function takeMostRecent(modelA, modelB) {
return (!modelA || modelA.modified === undefined) ? modelB :
(!modelB || modelB.modified === undefined) ? modelA :
modelA.modified > modelB.modified ? modelA :
modelB;
}
PersistedModelProvider.prototype.getModels = function (ids) {
var persistenceService = this.persistenceService,
$q = this.$q,
space = this.space;
spaces = this.spaces;
// Load a single object model from persistence
// Load a single object model from any persistence spaces
function loadModel(id) {
return persistenceService.readObject(space, id);
return $q.all(spaces.map(function (space) {
return persistenceService.readObject(space, id);
})).then(function (models) {
return models.reduce(takeMostRecent);
});
}
// Package the result as id->model