diff --git a/app/assets/javascripts/app/models/post.js b/app/assets/javascripts/app/models/post.js index 0d60fd013..d4b315cb3 100644 --- a/app/assets/javascripts/app/models/post.js +++ b/app/assets/javascripts/app/models/post.js @@ -68,6 +68,11 @@ app.models.Post = Backbone.Model.extend(_.extend({}, app.models.formatDateMixin, } }, + toggleFavorite : function(){ + this.set({favorite : !this.get("favorite")}) + this.save() + }, + like : function() { var self = this; this.likes.create({}, {success : function(resp){ diff --git a/app/assets/javascripts/app/models/stream.js b/app/assets/javascripts/app/models/stream.js index 459c71056..d48086f54 100644 --- a/app/assets/javascripts/app/models/stream.js +++ b/app/assets/javascripts/app/models/stream.js @@ -61,6 +61,13 @@ app.models.Stream = Backbone.Collection.extend({ this.items.add(models) }, + preloadOrFetch : function(){ //hai, plz test me THNX + this.preload() + if(this.items.length == 0) { + this.fetch() + } + }, + preload : function(){ var preloadJson = window.preLoadContent && JSON.parse(window.preLoadContent) delete window.preLoadContent // always do this just to be safe in preventing dirty state across navigates diff --git a/app/assets/javascripts/app/pages/profile.js b/app/assets/javascripts/app/pages/profile.js index f855d54ef..4920051c9 100644 --- a/app/assets/javascripts/app/pages/profile.js +++ b/app/assets/javascripts/app/pages/profile.js @@ -26,7 +26,7 @@ app.pages.Profile = app.views.Base.extend({ initialize : function(options) { this.model = new app.models.Profile.findByGuid(options.personId) this.stream = options && options.stream || new app.models.Stream() - this.stream.preload() + this.stream.preloadOrFetch(); this.canvasView = new app.views.Canvas({ model : this.stream }) this.profileInfo = new app.views.ProfileInfo({ model : this.model }) diff --git a/app/assets/javascripts/app/views/canvas_view.js b/app/assets/javascripts/app/views/canvas_view.js index 6ef14ac5d..4028dfc4a 100644 --- a/app/assets/javascripts/app/views/canvas_view.js +++ b/app/assets/javascripts/app/views/canvas_view.js @@ -4,6 +4,7 @@ app.views.Canvas = app.views.Base.extend(_.extend({}, app.views.infiniteScrollMi this.collection = this.stream.items this.postClass = app.views.SmallFrame this.setupInfiniteScroll() + this.stream.bind("reLayout", this.reRender, this) }, renderTemplate : function() { @@ -18,12 +19,16 @@ app.views.Canvas = app.views.Base.extend(_.extend({}, app.views.infiniteScrollMi _.defer(_.bind(function(){ this.$el.isotope("insert", this.createPostView(post).render().$el) }, this)) }, - mason : function() { + mason : function() { this.$el.isotope({ itemSelector : '.canvas-frame', masonry : { columnWidth : 292.5 } }) + }, + + reRender : function(){ + this.$el.isotope("reLayout") } })); diff --git a/app/assets/javascripts/app/views/small_frame.js b/app/assets/javascripts/app/views/small_frame.js index b606db87f..f5583d693 100644 --- a/app/assets/javascripts/app/views/small_frame.js +++ b/app/assets/javascripts/app/views/small_frame.js @@ -72,12 +72,13 @@ app.views.SmallFrame = app.views.Base.extend({ favoritePost : function(evt) { if(evt) { evt.stopImmediatePropagation(); evt.preventDefault() } - if(this.model.get("favorite")) { - this.model.save({favorite : false}) - } else { - this.model.save({favorite : true}) - this.$el.addClass("x2 width height") - } + var prevDimension = this.dimensionsClass(); + this.model.toggleFavorite(); + + this.$el.removeClass(prevDimension) + this.render() + + app.page.stream.trigger("reLayout") }, goToPost : function() { diff --git a/spec/javascripts/app/pages/profile_spec.js b/spec/javascripts/app/pages/profile_spec.js index 23db4a1bb..c3f26b247 100644 --- a/spec/javascripts/app/pages/profile_spec.js +++ b/spec/javascripts/app/pages/profile_spec.js @@ -1,7 +1,7 @@ describe("app.pages.Profile", function(){ beforeEach(function(){ this.guid = 'abcdefg123' - this.page = new app.pages.Profile({personId :this.guid }); + app.page = this.page = new app.pages.Profile({personId :this.guid }); this.stream = this.page.stream }); @@ -26,8 +26,27 @@ describe("app.pages.Profile", function(){ describe("rendering", function(){ beforeEach(function(){ - this.page.render(); + this.post = factory.post() + this.stream.add(this.post) + this.page.toggleEdit() + expect(this.page.editMode).toBeTruthy() + this.page.render() }); + + context("clicking fav", function(){ + beforeEach(function(){ + spyOn(this.post, 'toggleFavorite') + spyOn($.fn, "isotope") + this.page.$(".fav").click() + }) + + it("relayouts the page", function(){ + expect($.fn.isotope).toHaveBeenCalledWith("reLayout") + }) + it("toggles the favorite status on the model", function(){ + expect(this.post.toggleFavorite).toHaveBeenCalled() + }) + }) }); describe("edit mode", function(){