Created
April 25, 2012 14:46
Revisions
-
toolmantim revised this gist
Apr 25, 2012 . 1 changed file with 7 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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: // // MyApp.HeaderView = Backbone.View.extend({ // events: { // "click a.home": "home" // }, // home: function(e) { // return MyApp.router.navigateToLink(e); // } // }) navigateToLink: function(e, trigger) { if (arguments.length != 2) trigger = true; -
toolmantim revised this gist
Apr 25, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); e.preventDefault(); } } }); -
toolmantim revised this gist
Apr 25, 2012 . 1 changed file with 42 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); } } }); -
toolmantim created this gist
Apr 25, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()