quirkey / sammy

Sammy is a tiny javascript framework built on top of jQuery, It's RESTful Evented Javascript.
http://sammyjs.org
MIT License
2.99k stars 384 forks source link

404 Not Found event or Catch-all route #67

Closed lgomez closed 13 years ago

lgomez commented 13 years ago

Is there a way for me to set a 404 route or a catch-al route for when none of my defined routes match?

quirkey commented 13 years ago

Yes. You can override notFound(): http://code.quirkey.com/sammy/docs/api.html#Sammy.Application-notFound https://github.com/quirkey/sammy/blob/master/lib/sammy.js#L1141

lgomez commented 13 years ago

That worked perfectly.

Did you consider adding a something like '404' as a route instead of requiring an override? Not a big deal. Just curious.

Thanks!

wjcrowcroft commented 13 years ago

Hey, I'm looking for the same thing myself - had a look through the docs, but still not sure where I need to override notFound().

If you have time, could you please explain how and where to override it? Thanks!!

lgomez commented 13 years ago

Sure, in my case I did something similar to:

var app = $.sammy('#somediv', function() {
  this.get('#/somehash', function(context) {
    // do somethihnng
  });
  this.notFound = function(){
    // do something
  }
});

I hope that helps.

wjcrowcroft commented 13 years ago

Thanks! Yeah that clears it up. This worked great for me as an example:

    this.notFound = function(){
        this.swap('');
        $("<p>Oops, that couldn't be found</p>").appendTo(this.$element());
    }

Wonder how this impacts SEO - whether these 404s are treated like normal HTML 404s :S

lgomez commented 13 years ago

@josscrowcroft So far I've only used sammy for apps behind a login to avoid dealing with SEO and other issues like that. There are ways to work around them but I prefer to avoid them and keep it simple.

wjcrowcroft commented 13 years ago

Fair play. Cheers!

paulbhartzog commented 12 years ago

I use notFound to trigger a jQuery UI modal, or an internal message. Handy, thx. :-)