Created
November 14, 2012 20:08
-
-
Save Wizek/4074444 to your computer and use it in GitHub Desktop.
Controller-mixin pattern
This file contains hidden or 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
/*\ | |
* Controller-mixin pattern | |
\*/ | |
// At it's simplest: | |
myModule | |
.controller('aCtrl', function($scope, aCtrlMixin1, aCtrlMixin2, condition) { | |
if (condition) { | |
aCtrlMixin1($scope) | |
} else { | |
aCtrlMixin2($scope) | |
} | |
$scope.sharedStuff = angular.noop | |
}) | |
.value('aCtrlMixin1', function($scope) { | |
$scope.something1 = angular.noop | |
}) | |
.value('aCtrlMixin2', function($scope) { | |
$scope.something2 = angular.noop | |
}) | |
// Some stuff can be abstracted out to factories: | |
myModule | |
.controller('aCtrl', function($scope, aCtrlMixins) { | |
aCtrlMixins($scope) | |
$scope.sharedStuff = angular.noop | |
}) | |
.factory('aCtrlMixins', function(aCtrlMixin1, aCtrlMixin2, condition) { | |
return condition | |
? aCtrlMixin1 | |
: aCtrlMixin2 | |
}) | |
.value('aCtrlMixin1', function($scope) { | |
$scope.something1 = angular.noop | |
}) | |
.value('aCtrlMixin2', function($scope) { | |
$scope.something2 = angular.noop | |
}) | |
// Or even more so: | |
myModule | |
.controller('aCtrl', function($scope, aCtrlMixins) { | |
aCtrlMixins($scope) | |
}) | |
.factory('aCtrlMixins', function(aCtrlMixin1, aCtrlMixin2, condition, aCtrlMixin1and2both) { | |
var toBeApplied = condition | |
? aCtrlMixin1 | |
: aCtrlMixin2 | |
return function($scope) { | |
toBeApplied($scope) | |
aCtrlMixin1and2both($scope) | |
} | |
}) | |
.value('aCtrlMixin1and2both', function($scope) { | |
$scope.sharedStuff = angular.noop | |
}) | |
.value('aCtrlMixin1', function($scope) { | |
$scope.something1 = angular.noop | |
}) | |
.value('aCtrlMixin2', function($scope) { | |
$scope.something2 = angular.noop | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment