Skip to content

Commit

Permalink
DRY app/router.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen van Bergerem authored and jhass committed Oct 9, 2015
1 parent dccad3f commit f7bd0bb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 45 deletions.
74 changes: 33 additions & 41 deletions app/assets/javascripts/app/router.js
Expand Up @@ -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",
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -137,35 +118,19 @@ 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();
},

aspects_stream : function(){
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() {
Expand All @@ -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
3 changes: 0 additions & 3 deletions config/routes.rb
Expand Up @@ -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"
Expand Down
69 changes: 68 additions & 1 deletion spec/javascripts/app/router_spec.js
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
});
});
});

0 comments on commit f7bd0bb

Please sign in to comment.