Fire a parent route on subroute.

99 views
Skip to first unread message

Bruno Lazzaro

unread,
Nov 8, 2012, 2:21:20 PM11/8/12
to sam...@googlegroups.com
I have this two routes:

"#!/food" and "#!/food/sides"

On the first one i do a few things to sort some dom elements and stuff, and then on the second in theory, i just re filter the dom generated by the first one. The thing is that when i enter the second one the first one doesn't fire. I understand that, but i wanted to know if theres a way to make it fire without duplicating the code or having it in a single function.

Daniel Dotsenko

unread,
Nov 8, 2012, 4:30:06 PM11/8/12
to sam...@googlegroups.com
My 2 cents: Unite the two routes into one and use "splat"

    sammyapp.route('get',/\#\/(\d+)\/(.+)\//, function() {
        // splat is a key that holds the array of string fragments matched by the route regex that got us here.
        // everything with parens in regex above will show up in splat.
        // I don't know why "splat"
        var sectionname = this.params['splat'][0]
        , viewname = this.params['splat'][1]

This way you kill two birds with one stone - Centralized place to get the data and render for all view variations + very dynamic view.

Daniel

deitch

unread,
Nov 9, 2012, 6:20:30 AM11/9/12
to sam...@googlegroups.com
I have always liked the ability to do 2 things (neither of which Sammy does). I would think about adding it, but I still have an open pull request on having chained routes see https://github.com/quirkey/sammy/pull/170

1) As you said, have multiple routes match
2) Have hierarchical routes, something like:

this.get("/food",function(){
  // part A
  this.get("/sides",function(){
     // part B
  });
});

So part A gets launched for /food and /food/sides, but part B gets launched only for /food/sides

The above is probably a miserable interface, could be designed much better (i.e. easier to understand), but I like the idea, it keeps it much cleaner.
Reply all
Reply to author
Forward
0 new messages