Skip to content

Instantly share code, notes, and snippets.

@bmavity
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmavity/1150f53e0239a283353c to your computer and use it in GitHub Desktop.
Save bmavity/1150f53e0239a283353c to your computer and use it in GitHub Desktop.
Aggregate Mixin
function asAggregate() {
this.applyChange = function(evt, eventName, isFromHistory) {
var aggregate = this
var handler = aggregate[eventName]
if(handler) {
handler.call(aggregate, evt)
}
if(!isFromHistory) {
this._uncommittedChanges.push(evt)
}
}
this.commitChanges = function() {
this._uncommittedChanges = []
}
this.getUncommittedChanges = function() {
return this._uncommittedChanges
}
this.loadFromHistory = function(history) {
var aggregate = this
history.forEach(function(evt) {
aggregate.applyChange(evt.evt, evt.__type, true)
})
}
this.loadFromSnapshot = function(snapshot) {
var aggregate = this
Object.keys(snapshot).forEach(function(key) {
var val = snapshot[key]
aggregate[key] = val
})
}
}
// Usage
function AggregateInstance() {
this._uncommittedChanges = []
}
asAggregate.call(AggregateInstance.prototype)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment