javascript - Ideas for refactoring some duplicated code in peer angularjs controllers for managing $intervals -


I have a small SPA that displays many tabs. Each tab shows the operating status information about the application that it is concerned. The controllers of each tab create the $ interval object that calls AJAX to the server to receive status information.

To be a good citizen, I have concluded that when a tab appears, other gaps for the other tab should be disabled so that they do not call AJAX.

In HTML, each of my "tabs" elements has the same characteristics as the following:

   

select one of the tabs associated with that tab, I have There is a controller for the tab, but it has an Ardi with two entries, and in I use "$ broadcast" so that epicodriel entries can manage their own intervals.

Do not I think that I have fewer methods that are almost repeated in every controller. Each controller has the "selected" and "unselected" method, and all the components that manage their own intervals are "start" and "cancel" methods. In most tabs, those four tabs are in the controller. For a tab using "$ broadcast", I have "selected" and "unselected" methods in the tab controller and "start" and "cancel" methods in the ArDion controllers.

I wish I could define "selected" and "unselected" in the parent controller of all tabs, but for doing so, by adding some small "registration" code in the HTML to those methods Related to the "Call" Tab Controller

However, it is possible that the duplication of repetition in the controller will end in HTML with some weird looking codes.

Any ideas for how to do this better?

What I do is add a supportive service that configures my controller something like this:

  angular.module ('myModule'). Service ("control helper", function) {this.configController = function (controller) {controller.oneCommonMethod = function () {/ * some general code * controller. Communication method = function () {/ * some more general code;}}}}); Angular Module ('myModule') controller ("myController", function ($ radius, control helper) {controllerHelper.configController (this);});  

You can also share the status between the controllers who call this support service to complete what you want.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -