Created
February 22, 2012 05:20
-
-
Save zachlendon/1881650 to your computer and use it in GitHub Desktop.
backbone pushstate version
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 characters
| //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