How to construct Controller name from route

58 views
Skip to first unread message

Prem

unread,
Nov 4, 2014, 7:22:41 AM11/4/14
to ang...@googlegroups.com
I am trying to define a generic route which will handle most of request. Adding a route for each and every feature addition is what I feel not good and not maintainable.

So my target is to write a generic route and resolve the decencies dynamically, which include resolving all dependent controller & service files, resolving templateUrl and resolving controller name. I can resolve/construct everything except controller name. Please help me a way around

My route with a custom resolver:

 $routeProvider
 
.when('/:module/:controller/:action?', routeResolver.resolve())


Inside my custom resolver:

function routeResolverProvider(){

this.$get= function(){
 
return this;
}

this.resolve = function (options) {
 
var route = {};

 route
.templateUrl = function (params) {
     
var path = String.format('/app/components/{0}/{1}.view.html', params.module, params.controller);
     
return path;
 
};

 
//================================================================================
 route
.controller='THIS IS WHAT I WANT TO CONSTRUCT FROM ROUTE as templateUrl'  
 
//================================================================================

 route
.resolve = {
   loadDependencies
: ['$q', '$rootScope', '$route', function ($q, $rootScope, $route) {
     
// MY RESOLVE LOGIC HERE
     
// I construct dependent file path from route values
     
// String.format('/app/components/{0}/{1}.controller.js', params.module, params.controller);
   
}]
 
};

 
return route;
 
}}

app
.provider('routeResolver', routeResolverProvider)



Thanks,
Prem




Prem

unread,
Nov 5, 2014, 2:21:50 AM11/5/14
to ang...@googlegroups.com
Finally I solved my issue by instantiating controller manually. Now my resolver will look like 

this.$get = ['$rootScope', '$scope', '$route', '$controller', function () {
    return this;
}];

route.controller = function ($rootScope, $scope, $route, $controller) {
    var params = $route.current.params;
    $controller(params.controller.toLowerCase() + 'Controller', {$scope: $scope});
}


Reply all
Reply to author
Forward
0 new messages