Created
February 24, 2015 22:13
-
-
Save Ocramius/6a9846ac93ce2e0c6086 to your computer and use it in GitHub Desktop.
EventSourcing for Angular Repository actions reflows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (exports) { | |
function EventBus() { | |
this.handlers = []; | |
} | |
EventBus.prototype.push = function (event) { | |
foreach (idx in this.handlers) { | |
if (this.handler[idx].canHandle(event)) { | |
this.handler[idx].handle(event); | |
} | |
} | |
}; | |
EventBus.prototype.register = function (handler) { | |
this.handlers.push(handler); | |
}; | |
exports.EventBus = EventBus; | |
}()); | |
(function (exports) { | |
function FormulaRepository(eventBus) { | |
this.eventBus = eventBus; | |
this.formulas = []; | |
} | |
FormulaRepository.prototype.addFormula = function (formula) { | |
this.formulas[formula.id] = formula; | |
this.eventBus.push({ | |
name: 'formula-was-added', | |
data: formula | |
}); | |
}; | |
}()); | |
(function (exports) { | |
function FormulaController(eventBus, $scope) { | |
eventBus.register({ | |
canHandle: function (event) { | |
return event.name === 'formula-was-added' || | |
event.name === 'formula-was-modified' || | |
event.name === 'formula-was-removed'; | |
}, | |
handle: function (event) { | |
$scope.$apply(); | |
} | |
}); | |
} | |
}()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
foreach on line 7 causes an error. Are you thinking of PHP? Should "for" be used?