dotJEM / angular-routing

Enhanced state based routing for Angular applications!
https://dotjem.github.io/angular-routing/
MIT License
75 stars 9 forks source link

State with no template #55

Closed soundstep closed 11 years ago

soundstep commented 11 years ago

It would be nice to have a way of setting states that doesn't load templates but just instantiate a controller.

In the following example, navigate to /main would show the template, resolve the data and create the controller.

Navigate to /main/sub would only create the controller, this would be a great freedom.

.state('main', {
    route: '/main',
    views: {
        'main': {
            template: 'main.html',
            controller: 'MainController'
        }
    },
    resolve: {... load data here...}
})

.state('main.sub', {
    route: '/:sub',
    views: {
        'whatever': {
            controller: function() {

            }
        }
    },
    resolve: {... load data here...}
});

I had to do this task as the sub route was supposed to open a bootstrap modal window. In this case I don't want to create any html, I just want to handle a route and execute custom code to show the modal window.

I solve the problem loading an empty template in an empty hidden view, not ideal.

Is that already possible? if not, it would be a useful addition.

jeme commented 11 years ago

Try to look at transitions if that fits your purpose.

jeme commented 11 years ago

To elaborate a bit, start here: https://github.com/angular-ui/ui-router/issues/133

UI Route has actually had the same discussions about controller with no views, but this isn't feasible as the implementation is, also I don't think it makes sense because controllers are a thing from MVC, so they are tightly bound with a View and a Model.

However what we have instead is "actions on state" which is what transitions really mostly are... It would be neat with a more simple syntax maybe, but the behind the scenes wouldn't be much different.

With Angular Routing you have to additional option to hook in at different stages of the actual transition.

There is some lack of resolved values and also the ability to maybe inject them into "transitions" somehow... I will have a look at that...

As for your scenario on only running the controller again, this is where Refresh comes into the picture, again there is a missing link to resolve though, but you can go in and call refresh on a controller, if you wrap all you controller logic inside this, it would simulate that behavior quite well.

.state('main', {
    route: '/main',
    views: {
        'main': {
            sticky: "42",
            template: 'main.html',
            controller: 'MainController'
        }
    },
    resolve: {... load data here...}
})

.state('main.sub', {
    route: '/:sub',
    views: {
        'whatever': {
            sticky: "42",
            template: 'main.html',
            controller: 'MainController'

            }
        }
    },
    resolve: {... load data here...}
});