Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reversing Routes #34

Closed
airtonix opened this issue Mar 28, 2014 · 2 comments
Closed

Reversing Routes #34

airtonix opened this issue Mar 28, 2014 · 2 comments

Comments

@airtonix
Copy link

One of the things that always frustrated me about ngRoute was that I ended up hardcoding paths in my codebase. While this may be less typing initially, in the long run it leads to code that requires more work to maintain and integrate into other projects.

Coming from the Django world, where a Django project is made up of features modules, each describing their own url routes I became used to the concept of giving each route a shortname. Throughout my codebase (both project and the feature module) I would then build a url based on the short name and some parameters.

Fast forward to AngularJs, I created angular-named-routes. This works fine if you're only use ngRoutes, however I would like to utilise the nested view functionality of angular-route-segment.

Which leads me to my current situation: route-segment only exposes the first level of routes to ngRoute which means ngRoute has no idea what the path for a second level route might be.

I've created a gist with an attempt to illustrate the use case and some the start of a service that helps reverse a segment name into the full path:

https://gist.github.com/airtonix/9835196

@artch
Copy link
Owner

artch commented Mar 28, 2014

Have you tried to define the routes in each module separately rather than in the initial config all together? Like this:

angular.module('app.profile.main', ['route-segment', 'view-segment'])

.config(function($routeSegmentProvider) {
    $routeSegmentProvider.when('/:user/', 'main.profile')
    .within('main').segment('profile', {
        templateUrl: '/views/partials/profile/index.html',
        controller: 'profile:Main.IndexPage as IndexPage',
        dependencies: ['user']
    })
})

.controller('profile:Main.IndexPage', function() {
    ...
})

@airtonix
Copy link
Author

Yes, on the project I'm working on the various features are separated out into their own modules with their own route definitions.

But I think I made this issue ticket in error. After creating this ticket, I returned to my code and looked at how I was defining the routes. Seems to work after I removed any dashes - from the segment shortnames.

Resulting service seems to work nicely:

require [
    'angular'
    'lodash'
    ], (angular, _) ->

        angular.module 'app.common.services.routeResolver', [
            'ngRoute',
            'route-segment',
        ]

            .provider '$routeResolver', [
                '$locationProvider',
                ($locationProvider) ->
                    prefix = $locationProvider.hashPrefix()
                    this.$get = [
                        '$route',
                        ($route) ->
                            service =

                                resolve: (name, params) ->
                                    if not name
                                        return ''
                                    route = _.find $route.routes, {segment: name}

                                    if route
                                        output = route.originalPath

                                        if route.keys and params and _.isObject params
                                            _.forEach route.keys, (key) ->
                                                output = output.replace ":"+key.name, params[key.name]

                                        return prefix + output

                    ]

                    this
            ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants