Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save zachlendon/1881657 to your computer and use it in GitHub Desktop.
backbone hashbang 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();
//Backbone Router
App.Routers.TodosRouter = Backbone.Router.extend({
routes: {
":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('#' + this.model.toJSON().id);
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