diff --git a/app/assets/javascripts/app/app.js b/app/assets/javascripts/app/app.js index 21849fcac..4e361e477 100644 --- a/app/assets/javascripts/app/app.js +++ b/app/assets/javascripts/app/app.js @@ -42,7 +42,7 @@ var app = { Backbone.history.start({pushState: true}); // there's probably a better way to do this... - $("a[rel=backbone]").bind("click", function(evt){ + $("a[rel=backbone]").live("click", function(evt){ evt.preventDefault(); var link = $(this); diff --git a/app/assets/javascripts/app/pages/post-viewer.js b/app/assets/javascripts/app/pages/post-viewer.js index d1213e3c7..e58f26b90 100644 --- a/app/assets/javascripts/app/pages/post-viewer.js +++ b/app/assets/javascripts/app/pages/post-viewer.js @@ -11,12 +11,7 @@ app.pages.PostViewer = app.views.Base.extend({ initialize : function(options) { this.model = new app.models.Post({ id : options.id }); this.model.preloadOrFetch().done(_.bind(this.initViews, this)); - - this.prepIdleHooks(); - - $(document).bind("keypress", _.bind(this.commentAnywhere, this)) - $(document).bind("keypress", _.bind(this.invokePane, this)) - $(document).bind("keyup", _.bind(this.closePane, this)) + this.bindEvents() }, initViews : function() { @@ -29,6 +24,23 @@ app.pages.PostViewer = app.views.Base.extend({ this.render(); }, + bindEvents : function(){ + this.prepIdleHooks(); + this.bindNavHooks(); + + $(document).bind("keypress", _.bind(this.commentAnywhere, this)) + $(document).bind("keypress", _.bind(this.invokePane, this)) + $(document).bind("keyup", _.bind(this.closePane, this)) + }, + + unbind : function(){ + $(document).unbind("idle.idleTimer") + $(document).unbind("active.idleTimer") + $(document).unbind('keydown') + $(document).unbind('keypress') + $(document).unbind('keyup') + }, + prepIdleHooks : function () { $.idleTimer(3000); @@ -41,14 +53,6 @@ app.pages.PostViewer = app.views.Base.extend({ }); }, - postRenderTemplate : function() { - /* set the document title, if it has one */ - if(this.model.get("title")){ - document.title = this.model.get("title"); - } - this.bindNavHooks(); - }, - bindNavHooks : function() { var model = this.model; $(document).keydown(function(evt){ @@ -66,6 +70,10 @@ app.pages.PostViewer = app.views.Base.extend({ }) }, + postRenderTemplate : function() { + if(this.model.get("title")){ document.title = this.model.get("title"); } + }, + commentAnywhere : function(evt) { /* ignore enter, space bar, arrow keys */ if(_.include([13, 32, 37, 38, 39, 40], evt.keyCode)) { return } diff --git a/app/assets/javascripts/app/router.js b/app/assets/javascripts/app/router.js index fad9951ab..1af163bc0 100644 --- a/app/assets/javascripts/app/router.js +++ b/app/assets/javascripts/app/router.js @@ -42,19 +42,19 @@ app.Router = Backbone.Router.extend({ }, newProfile : function(personId) { - this.renderPage(new app.pages.Profile({ personId : personId })); + this.renderPage(function(){ return new app.pages.Profile({ personId : personId })}); }, composer : function(){ - this.renderPage(new app.pages.Composer()); + this.renderPage(function(){ return new app.pages.Composer()}); }, framer : function(){ - this.renderPage(new app.pages.Framer()); + this.renderPage(function(){ return new app.pages.Framer()}); }, singlePost : function(id) { - this.renderPage(new app.pages.PostViewer({ id: id })); + this.renderPage(function(){ return new app.pages.PostViewer({ id: id })}); }, profile : function(page) { @@ -85,8 +85,9 @@ app.Router = Backbone.Router.extend({ return query.search("ex=true") != -1 }, - renderPage : function(page){ - app.page = page + renderPage : function(pageConstructor){ + app.page && app.page.unbind && app.page.unbind() //old page might mutate global events $(document).keypress, so unbind before creating + app.page = pageConstructor() //create new page after the world is clean (like that will ever happen) $("#container").html(app.page.render().el) } });