0

In my .config I have a router that instantiate a pair controller-router:

angular.module('reporting', ['ng', 'ngRoute', 'ngResource', 'reporting.directives', 'reporting.controllers', 'reporting.config', 'ngGrid', 'ui.bootstrap'])
    .config(["$routeProvider", "$provide", function ($routeProvider, $provide) {
        $routeProvider
            .when('/dealersReq', {
                templateUrl: 'reporting/partials/dealersReqs.html',
                controller: 'DealersCtrl'
            })
            .when('/lmtReq', {
                templateUrl: 'reporting/partials/lmt.html',
                controller: 'lmtCtrl'
            })
            .when('/leadsCreated', {
                templateUrl: 'reporting/partials/leadsCreated.html',
                controller: 'LeadsCreatedCtrl'
            })
...

but each controller share the same initialization code (think about it like a constructor) that sets in the rootScope some variable like a title and other useful information for some controllers outside the <view>:

.controller('DealersCtrl', ['$scope','$rootScope', 'CONFIG',
    function($scope, $rootScope, CONFIG) {

        //////////// duplicated code

        var key = 'qtsldsCrtSncheQ';

        $rootScope.openReport.key = key;
        $rootScope.openReport.title = CONFIG.reports['' + key].title;

        //////////// duplicated code

        console.log('Initialized! Now I do what a controller should really do');
    }]);

What I would like to do is finding a way to move that code - which is duplicated into every controller at the moment - into something smarter and neater. Soemthing that the route can call during the routing instanciation for example. Of course each controller should have a different key, but that one could be exactly the controller name actually. I really don't know how to improve this. Any suggestion?

Bertuz
  • 2,390
  • 3
  • 25
  • 50
  • I've often felt that controllers should have a "before" function, some way that you can say, "run this code before you run any controller". In the "old" days, I used sammyjs for client-side routing, it supported a "before" function, which ran before each route's handler. – deitch Oct 05 '14 at 04:56

2 Answers2

0

Why don't create a method on the $rootScope which does that, and then call it from each controller, i.e.: $rootScope.init().

Kamil R
  • 447
  • 5
  • 11
  • what I would like to avoid is making a call in each Controller. Sooner or later I'll forget it! – Bertuz Oct 03 '14 at 09:36
0

You could use a Service for shared code but you should avoid to use $rootScope https://stackoverflow.com/a/16739309/3068081

Community
  • 1
  • 1
aitorllj93
  • 434
  • 3
  • 11
  • yup, I wrote $rootScope for the shake of clarity, but in actual fact there's a directive scope that encompasses everything I need – Bertuz Oct 03 '14 at 09:34
  • $service could do the job, yes, but I should remember to call that service in each `controller`. Not as much smart as I would like :-) – Bertuz Oct 03 '14 at 09:35
  • 1
    Yeah, in a past personal project I was looking something like a Controllers heritage but all the answers ended in a Service solution :S – aitorllj93 Oct 03 '14 at 09:40
  • a co-worker has suggested me a `decorator`. But he also said that "avoid it as much as you can" – Bertuz Oct 03 '14 at 09:41