Skip to content

Instantly share code, notes, and snippets.

@zachlendon
Created February 22, 2012 05:20
Show Gist options
  • Select an option

  • Save zachlendon/1881650 to your computer and use it in GitHub Desktop.

Select an option

Save zachlendon/1881650 to your computer and use it in GitHub Desktop.
backbone pushstate version
//document.ready() js function contains backbone.js instantiating code which:
$.mobile.pushStateEnabled = false;
new App.Routers.TodosRouter();
Backbone.history = Backbone.history || new Backbone.History({});
Backbone.history.start({pushState:true});
//Backbone Router
App.Routers.TodosRouter = Backbone.Router.extend({
routes: {
"/contextPath/:toDoId": "loadTodo"
},
initialize: function() {
this.todos = new App.Collections.Todos();
this.todoView = new App.Views.TodoView({collection: this.todos});
this.todos.fetch();
},
loadTodo: function(toDoId) {
//here you have your id and your todos collection
//you would use underscore and/or backbone collection functionality to get
//the model object (represented by model below) you want
...
this.todoView.setModelAndRender(model);
}
});
// The View
App.Views.TodoView = Backbone.View.extend({
el:'.content',
events: {
},
initialize:function () {
this.collection.bind('reset', this.navigateToTodo, this);
this.collection.bind('add', this.navigateToTodo, this);
},
navigateToTodo () {
Backbone.history.navigate('/todos/' + this.model.toJSON().id, {trigger: true});
return this;
},
setModelAndRender: function(model) {
this.model = model;
this.render();
return this;
},
render:function () {
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment