From f7bd0bbb24e9c21deda5f1ddadca8016ae320cc3 Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Fri, 9 Oct 2015 17:45:51 +0200 Subject: [PATCH] DRY app/router.js --- app/assets/javascripts/app/router.js | 74 +++++++++++++--------------- config/routes.rb | 3 -- spec/javascripts/app/router_spec.js | 69 +++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 45 deletions(-) diff --git a/app/assets/javascripts/app/router.js b/app/assets/javascripts/app/router.js index 170a7e184c1..5d3b3ff2a5e 100644 --- a/app/assets/javascripts/app/router.js +++ b/app/assets/javascripts/app/router.js @@ -10,17 +10,12 @@ app.Router = Backbone.Router.extend({ "user/edit": "settings", "users/sign_up": "registration", - //new hotness "posts/:id": "singlePost", "p/:id": "singlePost", - //oldness "activity": "stream", "stream": "stream", - "participate": "stream", - "explore": "stream", "aspects": "aspects", - "aspects/stream": "aspects_stream", "commented": "stream", "liked": "stream", "mentions": "stream", @@ -86,24 +81,10 @@ app.Router = Backbone.Router.extend({ } }, - //below here is oldness - stream : function() { - if(app.page) { - app.page.unbindInfScroll(); - app.page.remove(); - } app.stream = new app.models.Stream(); app.stream.fetch(); - app.page = new app.views.Stream({model : app.stream}); - app.publisher = app.publisher || new app.views.Publisher({collection : app.stream.items}); - app.shortcuts = app.shortcuts || new app.views.StreamShortcuts({el: $(document)}); - - var streamFacesView = new app.views.StreamFaces({collection : app.stream.items}); - - $("#main_stream").html(app.page.render().el); - $('#selected_aspect_contacts .content').html(streamFacesView.render().el); - this._hideInactiveStreamLists(); + this._initializeStreamView(); }, photos : function(guid) { @@ -137,10 +118,10 @@ app.Router = Backbone.Router.extend({ this._hideInactiveStreamLists(); }, - aspects : function(){ - app.aspects = new app.collections.Aspects(app.currentUser.get('aspects')); - this.aspects_list = new app.views.AspectsList({ collection: app.aspects }); - this.aspects_list.render(); + aspects: function() { + app.aspects = new app.collections.Aspects(app.currentUser.get("aspects")); + this.aspectsList = new app.views.AspectsList({ collection: app.aspects }); + this.aspectsList.render(); this.aspects_stream(); }, @@ -148,24 +129,8 @@ app.Router = Backbone.Router.extend({ var ids = app.aspects.selectedAspects('id'); app.stream = new app.models.StreamAspects([], { aspects_ids: ids }); app.stream.fetch(); - - app.page = new app.views.Stream({model : app.stream}); - app.publisher = app.publisher || new app.views.Publisher({collection : app.stream.items}); + this._initializeStreamView(); app.publisher.setSelectedAspects(ids); - - var streamFacesView = new app.views.StreamFaces({collection : app.stream.items}); - - $("#main_stream").html(app.page.render().el); - $('#selected_aspect_contacts .content').html(streamFacesView.render().el); - this._hideInactiveStreamLists(); - }, - - _hideInactiveStreamLists: function() { - if(this.aspects_list && Backbone.history.fragment !== "aspects") - this.aspects_list.hideAspectsList(); - - if(this.followedTagsView && Backbone.history.fragment !== "followed_tags") - this.followedTagsView.hideFollowedTags(); }, bookmarklet: function() { @@ -179,6 +144,33 @@ app.Router = Backbone.Router.extend({ this.renderPage(function() { return new app.pages.Profile({ el: $('body > .container-fluid') }); }); + }, + + _hideInactiveStreamLists: function() { + if(this.aspectsList && Backbone.history.fragment !== "aspects") { + this.aspectsList.hideAspectsList(); + } + + if(this.followedTagsView && Backbone.history.fragment !== "followed_tags") { + this.followedTagsView.hideFollowedTags(); + } + }, + + _initializeStreamView: function() { + if(app.page) { + app.page.unbindInfScroll(); + app.page.remove(); + } + + app.page = new app.views.Stream({model : app.stream}); + app.publisher = app.publisher || new app.views.Publisher({collection : app.stream.items}); + app.shortcuts = app.shortcuts || new app.views.StreamShortcuts({el: $(document)}); + + var streamFacesView = new app.views.StreamFaces({collection : app.stream.items}); + + $("#main_stream").html(app.page.render().el); + $("#selected_aspect_contacts .content").html(streamFacesView.render().el); + this._hideInactiveStreamLists(); } }); // @license-end diff --git a/config/routes.rb b/config/routes.rb index 775cf0092a9..fe9c1e1cc39 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,9 +50,6 @@ end # Streams - get "participate" => "streams#activity" # legacy - get "explore" => "streams#multi" # legacy - get "activity" => "streams#activity", :as => "activity_stream" get "stream" => "streams#multi", :as => "stream" get "public" => "streams#public", :as => "public_stream" diff --git a/spec/javascripts/app/router_spec.js b/spec/javascripts/app/router_spec.js index 259105d30b2..0dbe5b269e4 100644 --- a/spec/javascripts/app/router_spec.js +++ b/spec/javascripts/app/router_spec.js @@ -43,7 +43,7 @@ describe('app.Router', function () { factory.aspectAttrs() ]); var aspectsListView = new app.views.AspectsList({collection: aspects}).render(); - router.aspects_list = aspectsListView; + router.aspectsList = aspectsListView; expect(aspectsListView.$el.html()).not.toBe(""); router.stream(); @@ -61,6 +61,14 @@ describe('app.Router', function () { }); }); + describe("aspects", function() { + it("calls _initializeStreamView", function() { + spyOn(app.router, "_initializeStreamView"); + app.router.aspects(); + expect(app.router._initializeStreamView).toHaveBeenCalled(); + }); + }); + describe("bookmarklet", function() { it('routes to bookmarklet even if params have linefeeds', function() { var router = new app.Router(); @@ -70,4 +78,63 @@ describe('app.Router', function () { expect(route).toHaveBeenCalled(); }); }); + + describe("stream", function() { + it("calls _initializeStreamView", function() { + spyOn(app.router, "_initializeStreamView"); + app.router.stream(); + expect(app.router._initializeStreamView).toHaveBeenCalled(); + }); + }); + + describe("_initializeStreamView", function() { + beforeEach(function() { + delete app.page; + delete app.publisher; + delete app.shortcuts; + }); + + it("sets app.page", function() { + expect(app.page).toBeUndefined(); + app.router._initializeStreamView(); + expect(app.page).toBeDefined(); + }); + + it("sets app.publisher", function() { + expect(app.publisher).toBeUndefined(); + app.router._initializeStreamView(); + expect(app.publisher).toBeDefined(); + }); + + it("doesn't set app.publisher if already defined", function() { + app.publisher = { jasmineTestValue: 42 }; + app.router._initializeStreamView(); + expect(app.publisher.jasmineTestValue).toEqual(42); + }); + + it("sets app.shortcuts", function() { + expect(app.shortcuts).toBeUndefined(); + app.router._initializeStreamView(); + expect(app.shortcuts).toBeDefined(); + }); + + it("doesn't set app.shortcuts if already defined", function() { + app.shortcuts = { jasmineTestValue: 42 }; + app.router._initializeStreamView(); + expect(app.shortcuts.jasmineTestValue).toEqual(42); + }); + + it("unbinds inf scroll for old instances of app.page", function() { + var pageSpy = jasmine.createSpyObj("page", ["remove", "unbindInfScroll"]); + app.page = pageSpy; + app.router._initializeStreamView(); + expect(pageSpy.unbindInfScroll).toHaveBeenCalled(); + }); + + it("calls _hideInactiveStreamLists", function() { + spyOn(app.router, "_hideInactiveStreamLists"); + app.router._initializeStreamView(); + expect(app.router._hideInactiveStreamLists).toHaveBeenCalled(); + }); + }); });