2

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:

  1. Deploy the common code into a separate file and, using jQuery, include the file into both controllers,

  2. 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
   }

}
15
  • 3
    Can you post the controller code? Without seeing exactly what you're doing it's difficult to comment, but in general, the Angular way to do these kinds of things is to put the common code into a service and inject the service into your controllers. You could also use ES6 classes and define a common controller class with the shared behavior and then subclass it. Commented May 25, 2017 at 12:34
  • Thank you @MikeFeltman. In fact, the details of the code are not important in this post. What I'm thinking about is the concept of doing something that would behave like include without actually including anything. See? By passing the same parameters to the function as those of the controller (mainly $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? Commented May 25, 2017 at 12:41
  • Arguing with people about the code implies that you're not interested in the answer. If you ask a question and assume that somebody may know better than you about the subject, you have to trust their opinion (even if you don't trust, there are rules for the questions on SO). If there is code that can illustrate your case, please, post it and don't make the answerers beg for it. Describing code with words never works. Commented May 25, 2017 at 13:52
  • Dear @estus, first, I re-read my comment and couldn't find even a hint to an "argument". Second, the fact that I'm posting questions here should inspire the assumption that I do appreciate the fact that there are people around there that know better than me. Third (and last), please check the edit I made and (at least I hope) you may find yourself agreeing with me about the irrelevance of the actual code. Commented May 26, 2017 at 10:13
  • 1
    Ah, so the controllers are not completely identical, as you said until now. Delegating to a single function is the solution you think is the best. A better solution, IMHO would be two controller classes inheriting a base class. Or a component with an attribute whoAmI. Commented May 29, 2017 at 14:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.