Skip to content

Instantly share code, notes, and snippets.

@toolmantim
Created April 25, 2012 14:46

Revisions

  1. toolmantim revised this gist Apr 25, 2012. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions router.js
    Original file line number Diff line number Diff line change
    @@ -21,11 +21,14 @@ MyApp.Router = Backbone.Router.extend({
    //
    // Handy for using from within event handlers:
    //
    // class MyApp.HeaderView extends Backbone.View
    // events:
    // MyApp.HeaderView = Backbone.View.extend({
    // events: {
    // "click a.home": "home"
    // home: (e) ->
    // MyApp.router.navigateToLink(e)
    // },
    // home: function(e) {
    // return MyApp.router.navigateToLink(e);
    // }
    // })
    navigateToLink: function(e, trigger) {
    if (arguments.length != 2)
    trigger = true;
  2. toolmantim revised this gist Apr 25, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion router.js
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ MyApp.Router = Backbone.Router.extend({
    } else {
    // Backbone navigate paths don't start with a forward slash
    this.navigate(href.replace(/^\//,''), trigger);
    return e.preventDefault();
    e.preventDefault();
    }
    }
    });
  3. toolmantim revised this gist Apr 25, 2012. 1 changed file with 42 additions and 0 deletions.
    42 changes: 42 additions & 0 deletions router.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    MyApp.Router = Backbone.Router.extend({

    hasPushState: window.history && window.history.pushState,

    // Override the navigate function to remove Backbone's #hash fallback for
    // non-pushState browsers. Instead we just navigate to the new location using
    // window.location
    navigate: function(fragment, trigger) {
    if (this.hasPushState) {
    return Backbone.Router.prototype.navigate(fragment, trigger);
    } else {
    // Backbone navigate paths don't start with a forward slash, so we may
    // need to add one
    if (!fragment.match(/^\//)) fragment = "/" + fragment;
    window.location = fragment;
    }
    },

    // Navigates to the href property of an event's currentTarget whilst ignoring
    // right clicks, middle clicks, shift-clicks etc.
    //
    // Handy for using from within event handlers:
    //
    // class MyApp.HeaderView extends Backbone.View
    // events:
    // "click a.home": "home"
    // home: (e) ->
    // MyApp.router.navigateToLink(e)
    navigateToLink: function(e, trigger) {
    if (arguments.length != 2)
    trigger = true;
    if (e.which == 2 || e.metaKey || e.ctrlKey || e.shiftKey) {
    // Make sure we return true in case we're used as the return value for
    // the event handling function
    return true;
    } else {
    // Backbone navigate paths don't start with a forward slash
    this.navigate(href.replace(/^\//,''), trigger);
    return e.preventDefault();
    }
    }
    });
  4. toolmantim created this gist Apr 25, 2012.
    35 changes: 35 additions & 0 deletions router.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    class MyApp.Router extends Backbone.Router

    hasPushState: window.history and window.history.pushState

    # Override the navigate function to remove Backbone's #hash fallback for
    # non-pushState browsers. Instead we just navigate to the new location using
    # window.location
    navigate: (fragment, trigger) ->
    if @hasPushState
    super(arguments...)
    else
    # Backbone navigate paths don't start with a forward slash, so we may
    # need to add one
    fragment = "/#{fragment}" unless fragment.match(/^\//)
    window.location = fragment

    # Navigates to the href property of an event's currentTarget whilst ignoring
    # right clicks, middle clicks, shift-clicks etc.
    #
    # Handy for using from within event handlers:
    #
    # class MyApp.HeaderView extends Backbone.View
    # events:
    # "click a.home": "home"
    # home: (e) ->
    # MyApp.router.navigateToLink(e)
    navigateToLink: (e, trigger=true) ->
    if e.which == 2 or e.metaKey or e.ctrlKey or e.shiftKey
    # Make sure we return true in case we're used as the return value for
    # the event handling function
    return true
    else
    # Backbone navigate paths don't start with a forward slash
    @navigate href.replace(/^\//,''), trigger
    e.preventDefault()