Is it possible to override a route after the app has started?

157 views
Skip to first unread message

Rich Manalang

unread,
Nov 1, 2010, 8:03:19 PM11/1/10
to sam...@googlegroups.com
I'm building a plugin framework for my app where each plugin can declare its own routes.  I've been trying to override an existing route from a plugin, but it doesn't seem to work.  Is it even possible to do this?

Thanks!

Rich, @rmanalan

Aaron Quint

unread,
Nov 1, 2010, 8:10:34 PM11/1/10
to sam...@googlegroups.com
running doesn't have any affect on the routes (other then it starts checking them), this is more likely an issue of route order, routes are added to an array and checked in the order in which they were defined/added. this applies to plugins, too, so unless you use() you're plugin before the other routes it will always take the first one that matches. I could see a need for 'add this route to the top' - any ideas for an API? 
--AQ
--
You received this message because you are subscribed to the Google Groups "Sammy.js" group.
To post to this group, send email to sam...@googlegroups.com.
To unsubscribe from this group, send email to sammyjs+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sammyjs?hl=en.

manalang

unread,
Nov 1, 2010, 8:34:14 PM11/1/10
to Sammy.js
Actually, I'm not using the Sammy plugin framework. I'm just doing
something like this:

// MAIN APP
var myApp = $.sammy(function(app){
app.get('#/',function(){
...
});

app.get('#/:id/:page',function(){
...
})
}).run('#/');

Then in another js that's loaded async through the main '#/' route:

myApp.get('#/:id/:page',function(){
...do this instead
});

Here I was hoping that the route that got created last overrides the
one created earlier, but that doesn't seem to be the case.

Thoughts? Or maybe a better way of doing this?

On Nov 1, 5:10 pm, Aaron Quint <aa...@quirkey.com> wrote:
> running doesn't have any affect on the routes (other then it starts checking
> them), this is more likely an issue of route order, routes are added to an
> array and checked in the order in which they were defined/added. this
> applies to plugins, too, so unless you use() you're plugin before the other
> routes it will always take the first one that matches. I could see a need
> for 'add this route to the top' - any ideas for an API?
> --AQ
>
> Aaron Quinthttp://www.quirkey.com
>
> On Mon, Nov 1, 2010 at 5:03 PM, Rich Manalang <rich.manal...@gmail.com>wrote:
>
>
>
>
>
>
>
> > I'm building a plugin framework for my app where each plugin can declare
> > its own routes.  I've been trying to override an existing route from a
> > plugin, but it doesn't seem to work.  Is it even possible to do this?
>
> > Thanks!
>
> > Rich, @rmanalan
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Sammy.js" group.
> > To post to this group, send email to sam...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > sammyjs+u...@googlegroups.com<sammyjs%2Bunsu...@googlegroups.com >
> > .

Aaron Quint

unread,
Nov 2, 2010, 2:01:52 AM11/2/10
to sam...@googlegroups.com
You might benefit from using plugins as simple closures. 

var myApp = $.sammy(function(app){
 app.get('#/',function(){
   ...
 });

 app.get('#/:id/:page',function(){
   ...
 })
}).run('#/');


myApp.use(function() {
  this.get('#/:id/:page',function(){
     ...do this instead
  });
});

That still doesnt solve your problem, though. I guess what you're assuming (which is not entirely incorrect) is that routes are more like an object with paths as keys. So adding a route with the same path should update it. All paths in fact are just turned into RegExp's even if they start out as strings. I guess I could still compare the new RegExps to existing ones, but I have a feeling that might result in some strange behavior. Possibly worth a try, though. You could hack `route()` to add any route to the begining of the array instead of the end as well. Any other ideas?
--AQ
To unsubscribe from this group, send email to sammyjs+u...@googlegroups.com.

matty h!

unread,
Nov 2, 2010, 3:54:02 PM11/2/10
to Sammy.js
Hey, I think I am trying to do the same thing.

I want to replace a route callback in my app after it is running,
based on some info that is returned via ajax before

for example:
My app first loads available routes via json and maps them via
mapRoutes.
On some routes, an ajax call returns a url.
This url is to be used(by partial) in a new callback for the current
route.
I realize that I could just use the url directly, but I'd like the
route to continue being available after


I am not sure what function to use in order to get the route to re-run
after the replacement.

Here is my hack for a replaceRoute function in a new fork of Sammy.js:
http://github.com/misterinterrupt/sammy/commit/76a8abb60b95d5d8a7cefb5abf58256cf2917357

I am unsure whether this is actually working.. Could someone try to
use this function ? I am attempting to use this outside of the app in
an ajax callback like so:

http://pastebin.com/fU7yrihX


On Nov 1, 11:01 pm, Aaron Quint <aa...@quirkey.com> wrote:
> You might benefit from using plugins as simple closures.
>
> var myApp = $.sammy(function(app){
>  app.get('#/',function(){
>    ...
>  });
>
>  app.get('#/:id/:page',function(){
>    ...
>  })
>
> }).run('#/');
>
> myApp.use(function() {
>   this.get('#/:id/:page',function(){
>      ...do this instead
>   });
>
> });
>
> That still doesnt solve your problem, though. I guess what you're assuming
> (which is not entirely incorrect) is that routes are more like an object
> with paths as keys. So adding a route with the same path should update it.
> All paths in fact are just turned into RegExp's even if they start out as
> strings. I guess I could still compare the new RegExps to existing ones, but
> I have a feeling that might result in some strange behavior. Possibly worth
> a try, though. You could hack `route()` to add any route to the begining of
> the array instead of the end as well. Any other ideas?
> --AQ
>
> Aaron Quinthttp://www.quirkey.com
> > <sammyjs%2Bunsu...@googlegroups.com<sammyjs%252Buns...@googlegroups.com>>

Aaron Quint

unread,
Nov 2, 2010, 8:20:47 PM11/2/10
to sam...@googlegroups.com
matty h: theres a full test suite in test if you want to write some tests to cover the method. I think theres probably a quicker way to do this internally (by just splicing the correct route in there).

However, I think I have a better solution, which is just using named helpers and replacing those. For example:


var myApp = $.sammy(function() {

  this.helpers({
    loadIndex: function() {
      // you can do everything you would do in a route here.
    }
  });

  this.get('#/index', function() {
    this.loadIndex();
  });
  
})

// overriding:

myApp.helpers({
  loadIndex: function() {
    // new index
  }
})

// or as a plugin

var overrideApp = function(app) {
  app.helpers({
    loadIndex: function() { ... }
  });
}; 

myApp.use(overrideApp);
---

The more I think about this, the more I like it. Its kind of like a very simple inheritance.
--AQ
To unsubscribe from this group, send email to sammyjs+u...@googlegroups.com.

matty h!

unread,
Nov 3, 2010, 12:11:57 AM11/3/10
to Sammy.js
Thanks Aaron!
I think that I have been thinking inside the box, thanks for the clear
and creative solution!
I really like this, it fleshes out my thoughts of how to get down to
mo bizniz with Sammy!
This may just be what I use henceforth, but I will still take a gander
at the tests so that I am ready for next time. (or if I see how what I
was doing can work)

Matty
> Aaron Quinthttp://www.quirkey.com
>
> On Tue, Nov 2, 2010 at 12:54 PM, matty h! <misterinterr...@gmail.com> wrote:
> > Hey, I think I am trying to do the same thing.
>
> > I want to replace a route callback in my app after it is running,
> > based on some info that is returned via ajax before
>
> > for example:
> > My app first loads available routes via json and maps them via
> > mapRoutes.
> > On some routes, an ajax call returns a url.
> > This url is to be used(by partial) in a new callback for the current
> > route.
> > I realize that I could just use the url directly, but I'd like the
> > route to continue being available after
>
> > I am not sure what function to use in order to get the route to re-run
> > after the replacement.
>
> > Here is my hack for a replaceRoute function in a new fork of Sammy.js:
>
> >http://github.com/misterinterrupt/sammy/commit/76a8abb60b95d5d8a7cefb...
> > > > <sammyjs%2Bunsu...@googlegroups.com<sammyjs%252Buns...@googlegroups.com>
> > <sammyjs%252Buns...@googlegroups.com<sammyjs%25252Bun...@googlegroups.com>

Rich Manalang

unread,
Nov 3, 2010, 1:15:02 PM11/3/10
to sam...@googlegroups.com
This works well for me too.  I was actually doing something similar using binds and function detection.  This is much cleaner.  Thanks!

Ben Hoskings

unread,
Nov 3, 2010, 6:57:51 PM11/3/10
to sam...@googlegroups.com
Me too. I was handling helpers manually because I didn't realise this functionality was there. This is really nice, cheers :)

Ben

Sent from my iPad
Reply all
Reply to author
Forward
0 new messages