Skip to content

Instantly share code, notes, and snippets.

@sonnym
Created February 20, 2014 03:01
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 sonnym/9106358 to your computer and use it in GitHub Desktop.
Save sonnym/9106358 to your computer and use it in GitHub Desktop.
Controller mediator example
angular.module("FooBar").
service("ControllerMediator", function() {
var methodCache = {};
return {
registerMediator: function(name) {
methodCache[name] = { conditions: [] };
return this.run(name);
},
registerCondition: function(name, fn) {
methodCache[name].conditions.push(fn);
},
registerCallback: function(name, fn) {
methodCache[name].callback = fn;
},
run: function(name) {
var conditions = methodCache[name].conditions;
var result = true;
for(var i = 0, l = conditions.length; i < l; i++) {
result = result && conditions[i]();
}
if (result) {
methodCache[name].callback();
}
}
}
}).
controller("ParentController", function(ControllerMediator) {
$scope.something = ControllerMediator.registerMediator("foobar");
}).
controller("ChildControllerOne", function(ControllerMediator) {
ControllerMediator.registerCondition("foobar", function() {
this.getResult() > 0 && this.isNotBlocking();
})
}).
controller("ChildControllerOne", function(ControllerMediator) {
ControllerMediator.registerCallback("foobar", function() {
this.doSomeStuff();
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment