Created
February 20, 2014 03:01
-
-
Save sonnym/9106358 to your computer and use it in GitHub Desktop.
Controller mediator example
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
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