Skip to content

Instantly share code, notes, and snippets.

@Wizek
Created November 14, 2012 20:08
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Wizek/4074444 to your computer and use it in GitHub Desktop.
Save Wizek/4074444 to your computer and use it in GitHub Desktop.
Controller-mixin pattern
/*\
* 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