I have two divs, each requiring a controller to handle activities within it. Both controllers have different names of course.
This situation is problematic since it requires maintenance on two identical pieces of code.
I was considering two options and am seeking comments:
Deploy the common code into a separate file and, using jQuery, include the file into both controllers,
Create a new file with a function that would receive the same parameters as the controllers and that would contain the whole logic currently hosted within both controllers. Then, simply invoke this function from within both controllers.
In the second option, the body of the controllers would become a single statement, i.e. the invocation of the new function.
From posts I found, it looks that the first option does work. My question is (before I engage into significant changes and testing): Would the second option work as well? and if yes, is it better, equivalent or worse than the first?
Edit
Though, as explained, the actual code is of no relevance to my question, I'm adding a set of sample pieces to illustrate my question, especially the second option.
So here is the initial arrangement:
Controller 1
myApp.controller("First_Controller", "$rootScope", "$scope" , [function($rootScope , $scope) {
$scope.Do_Something = function () {
// some code here
}
}
Controller 2
myApp.controller("Second_Controller", "$rootScope", "$scope" , [function($rootScope , $scope) {
$scope.Do_Something = function () {
// some code here
}
}
The idea is to convert this to:
Controller 1 - NEW
myApp.controller("First_Controller", "$rootScope", "$scope" , [function($rootScope , $scope) {
Common_Function($rootScope , $scope) ;
}
Controller 2 - NEW
myApp.controller("Second_Controller", "$rootScope", "$scope" , [function($rootScope , $scope) {
Common_Function($rootScope , $scope) ;
}
Where:
Common_Function = function ($rootScope , $scope) {
$scope.Do_Something = function () {
// some code here
}
}
$scope), whatever the code does when within a controller would be done much the same if it is contained within the invoked function. Well... this is what I'm thinking about, and the question is: Does it make sense?