reLayouts when something is favorited, whoa

This commit is contained in:
Dennis Collinson 2012-04-23 12:34:52 -07:00
parent 85f2f13d79
commit e8cfe01c8a
6 changed files with 47 additions and 10 deletions

View file

@ -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() { like : function() {
var self = this; var self = this;
this.likes.create({}, {success : function(resp){ this.likes.create({}, {success : function(resp){

View file

@ -61,6 +61,13 @@ app.models.Stream = Backbone.Collection.extend({
this.items.add(models) this.items.add(models)
}, },
preloadOrFetch : function(){ //hai, plz test me THNX
this.preload()
if(this.items.length == 0) {
this.fetch()
}
},
preload : function(){ preload : function(){
var preloadJson = window.preLoadContent && JSON.parse(window.preLoadContent) var preloadJson = window.preLoadContent && JSON.parse(window.preLoadContent)
delete window.preLoadContent // always do this just to be safe in preventing dirty state across navigates delete window.preLoadContent // always do this just to be safe in preventing dirty state across navigates

View file

@ -26,7 +26,7 @@ app.pages.Profile = app.views.Base.extend({
initialize : function(options) { initialize : function(options) {
this.model = new app.models.Profile.findByGuid(options.personId) this.model = new app.models.Profile.findByGuid(options.personId)
this.stream = options && options.stream || new app.models.Stream() 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.canvasView = new app.views.Canvas({ model : this.stream })
this.profileInfo = new app.views.ProfileInfo({ model : this.model }) this.profileInfo = new app.views.ProfileInfo({ model : this.model })

View file

@ -4,6 +4,7 @@ app.views.Canvas = app.views.Base.extend(_.extend({}, app.views.infiniteScrollMi
this.collection = this.stream.items this.collection = this.stream.items
this.postClass = app.views.SmallFrame this.postClass = app.views.SmallFrame
this.setupInfiniteScroll() this.setupInfiniteScroll()
this.stream.bind("reLayout", this.reRender, this)
}, },
renderTemplate : function() { 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)) _.defer(_.bind(function(){ this.$el.isotope("insert", this.createPostView(post).render().$el) }, this))
}, },
mason : function() { mason : function() {
this.$el.isotope({ this.$el.isotope({
itemSelector : '.canvas-frame', itemSelector : '.canvas-frame',
masonry : { masonry : {
columnWidth : 292.5 columnWidth : 292.5
} }
}) })
},
reRender : function(){
this.$el.isotope("reLayout")
} }
})); }));

View file

@ -72,12 +72,13 @@ app.views.SmallFrame = app.views.Base.extend({
favoritePost : function(evt) { favoritePost : function(evt) {
if(evt) { evt.stopImmediatePropagation(); evt.preventDefault() } if(evt) { evt.stopImmediatePropagation(); evt.preventDefault() }
if(this.model.get("favorite")) { var prevDimension = this.dimensionsClass();
this.model.save({favorite : false}) this.model.toggleFavorite();
} else {
this.model.save({favorite : true}) this.$el.removeClass(prevDimension)
this.$el.addClass("x2 width height") this.render()
}
app.page.stream.trigger("reLayout")
}, },
goToPost : function() { goToPost : function() {

View file

@ -1,7 +1,7 @@
describe("app.pages.Profile", function(){ describe("app.pages.Profile", function(){
beforeEach(function(){ beforeEach(function(){
this.guid = 'abcdefg123' 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 this.stream = this.page.stream
}); });
@ -26,8 +26,27 @@ describe("app.pages.Profile", function(){
describe("rendering", function(){ describe("rendering", function(){
beforeEach(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(){ describe("edit mode", function(){