Given a Route.Path object, is there a way to get the Controller?

31 views
Skip to first unread message

Richard Flosi

unread,
Apr 25, 2014, 5:36:49 PM4/25/14
to spi...@googlegroups.com
I'm trying to make use of the 'before' event that is triggered on a route.
The callback on the 'before' event is called with a Path object.
I want to get the related Controller from this Path object.

Alternatively, I'd like to be able to setup the 'before' event listener on the Controller itself, but not sure if that is possible.

Richard Flosi

unread,
Apr 25, 2014, 6:37:37 PM4/25/14
to spi...@googlegroups.com
So this is what I came up with...

In my app/index.js:
    App = Spine.Controller.sub({
        init: function() {
            ...
            this.routes = new Routes({
                el: $('#app')
            });
            Spine.Route.bind('before', this.proxy(this.beforeNavigate));
            Spine.Route.setup({
                history: true
            });
        },
        beforeNavigate: function(path) {
            if (this.routes.controllers[this.routes.routes[path.path]].prototype.loginRequired) {
                console.log('require user is logged in.');
            } else {
                console.log('login not required.');
            }
        },
        ...
    });

And in a controller I do:
    MyController = Spine.Controller.sub({
        ...
        loginRequired: true,
        ...
    });

And Routes is just a Spine.Stack:
    Routes = Spine.Stack.sub({
        controllers: {
            mycontroller: require('mycontroller')
        },
        routes: {
            '/mycontroller': 'mycontroller'
        }
    });

This seems to work, but I'm open to suggestions.

Ideally I'd like to just be able to listen to the 'before' event in the context of a controller and not have to do all this lookup magic:
"this.routes.controllers[this.routes.routes[path.path]].prototype.loginRequired"

Richard Flosi

unread,
Apr 25, 2014, 6:56:24 PM4/25/14
to spi...@googlegroups.com
Ok. So, this is a lot easier than I made it out to be...

    MyController = Spine.Controller.sub({
        init: function() {
            Spine.Route.bind('before', this.proxy(this.before));
            ....
        },
        before: function(path) {
            // do stuff
        },
        ...
    });

Richard Flosi

unread,
Apr 28, 2014, 10:29:59 AM4/28/14
to spi...@googlegroups.com
My last post doesn't work too well either since 'before' will be triggered on all navigation.
I guess I'm trying to get 'before' to work like 'active()' works for a stack.
Maybe I should just use active.

Richard Flosi

unread,
Apr 28, 2014, 2:13:05 PM4/28/14
to spi...@googlegroups.com
I ended up ditching my attempt to use the `before` trigger.
If anyone is using `before` I'd be curious what you are using it for.
Thanks!
Reply all
Reply to author
Forward
0 new messages