Yesterday, I saw my co-worker write a huge controller with only one "god object" $scope
that should look like this following code.
myApp.controller('ComplexController', ['$scope', function($scope) {
$scope.firstTab = {
init: function () {
$scope.firstTab.data1.foo = $scope.firstTab.DefaultData1.foo;
$scope.$watch('firstTab.data1.foo', function (){
// do something
});
},
defaultData1: {
foo: 'bar'
},
data1: {
foo: 'bar',
publiclyUsedMethod1: function () {},
publiclyUsedMethod2: function () {},
privatelyUsedMethod1: function () {},
privatelyUsedMethod2: function () {},
privatelyUsedMethod3: function () {},
privatelyUsedMethod4: function () {},
privatelyUsedMethod5: function () {}
},
data2: {
// Same code pattern as above
},
data3: {
// Same code pattern as above
},
data4: {
// Same code pattern as above
}
};
$scope.secondTab = {
// Same code pattern as above
};
$scope.thirdTab = {
// Same code pattern as above
};
$scope.refresh = function(){
// do something
};
$scope.$watchCollection('[$scope.firstTab.data1.foo, $scope.secondTab.data1.foo, $scope.thirdTab.data1.foo]',function(newValues,oldValues){
// some logic
$scope.refresh();
});
$scope.firstTab.init();
$scope.secondTab.init();
$scope.thirdTab.init();
}]);
What do you think which this pattern? What's the main purpose of $scope
object? Is it OK to store every private and public object in $scope
object?
Thanks,