Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created October 15, 2011 21:46
Show Gist options
  • Select an option

  • Save mxriverlynn/1290199 to your computer and use it in GitHub Desktop.

Select an option

Save mxriverlynn/1290199 to your computer and use it in GitHub Desktop.
tips for using backbone routers with pushstate
<a href="#foo">Say "foo"</a><br/>
<a href="#bar">Say "bar"</a><br/>
<div id="output"></div>
MyRouter = Backbone.Router.extend({
routes: {
"foo": "foo",
"bar": "bar"
},
foo: function(){
$("#output").append("foo!<br/>");
},
bar: function(){
$("#output").append("bar!<br/>");
}
});
new MyRouter();
Backbone.history.start();
<a href="/foo">Say "foo"</a><br/>
<a href="/bar">Say "bar"</a><br/>
<div id="output"></div>
MyRouter = Backbone.Router.extend({
routes: {
"foo": "foo",
"bar": "bar"
},
foo: function(){
$("#output").append("foo!<br/>");
},
bar: function(){
$("#output").append("bar!<br/>");
}
});
new MyRouter();
Backbone.history.start({pushState: true});
<a href="/foo" class="foo">Say "foo"</a><br/>
<a href="/bar" class="bar">Say "bar"</a><br/>
<div id="output"></div>
MyView = Backbone.View.extend({
events: {
"click a.foo": "foo",
"click a.bar": "bar"
},
foo: function(e){
e.preventDefault();
history.navigate("foo");
$("#output").append("foo");
},
bar: function(e){
e.preventDefault();
history.navigate("bar");
$("#output").append("bar");
}
});
new MyView({el: $("body")});
history = new Backbone.History();
history.start({pushState: true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment