How to detect invalid route and trigger function in Backbone.Controller

Posted by tomodian on Stack Overflow See other posts from Stack Overflow or by tomodian
Published on 2011-06-27T06:01:58Z Indexed on 2011/06/27 8:22 UTC
Read the original article Hit count: 154

Filed under:

Is there any method to detect invalid (or undefined) route and trigger 404 page in Backbone.Controller?

I've defined routes in my Controller like this, but it didn't work.

class MyController extends Backbone.Controller
    routes:
        "method_a": "methodA"
        "method_b": "methodB"
        "*undefined": "show404Error"

    # when access to /#method_a
    methodA: ->
        console.log "this page exists"

    # when access to /#method_b
    methodB: ->
        console.log "this also exists"

    # when access to /#some_invalid_hash_fragment_for_malicious_attack
    show404Error: ->
        console.log "sorry, this page does not exist"

© Stack Overflow or respective owner

Related posts about backbone.js