[Search] Listen on a global mutation topic

Listen on a global mutation topic to remove the need to retain
listeners per domain object.
This commit is contained in:
Victor Woeltjen
2015-09-29 11:47:02 -07:00
parent ad7d3d642e
commit 866c8882ca
3 changed files with 34 additions and 30 deletions

View File

@ -29,7 +29,8 @@ define(
function () {
"use strict";
var TOPIC_PREFIX = "mutation:";
var GENERAL_TOPIC = "mutation",
TOPIC_PREFIX = "mutation:";
// Utility function to overwrite a destination object
// with the contents of a source object.
@ -78,7 +79,11 @@ define(
* @implements {Capability}
*/
function MutationCapability(topic, now, domainObject) {
this.mutationTopic = topic(TOPIC_PREFIX + domainObject.getId());
this.generalMutationTopic =
topic(GENERAL_TOPIC);
this.specificMutationTopic =
topic(TOPIC_PREFIX + domainObject.getId());
this.now = now;
this.domainObject = domainObject;
}
@ -115,11 +120,19 @@ define(
// mutator function has a temporary copy to work with.
var domainObject = this.domainObject,
now = this.now,
t = this.mutationTopic,
generalTopic = this.generalMutationTopic,
specificTopic = this.specificMutationTopic,
model = domainObject.getModel(),
clone = JSON.parse(JSON.stringify(model)),
useTimestamp = arguments.length > 1;
function notifyListeners(model) {
// Broadcast a general event...
generalTopic.notify(domainObject);
// ...and also notify listeners watching this specific object.
specificTopic.notify(model);
}
// Function to handle copying values to the actual
function handleMutation(mutationResult) {
// If mutation result was undefined, just use
@ -136,7 +149,7 @@ define(
copyValues(model, result);
}
model.modified = useTimestamp ? timestamp : now();
t.notify(model);
notifyListeners(model);
}
// Report the result of the mutation