fix the "has no posts" on back bug.

This commit is contained in:
Dennis Collinson 2012-05-24 11:39:58 -07:00
parent 946f78b025
commit f3174941bd
4 changed files with 28 additions and 20 deletions

View file

@ -3,16 +3,18 @@ app.models.Profile = Backbone.Model.extend({
}, {
preloadOrFetch : function(id){
if(app.hasPreload("person")) {
return new app.models.Profile(app.parsePreload("person"))
} else {
return this.findByGuid(id)
}
return app.hasPreload("person") ? this.preload() : this.findByGuid(id)
},
preload : function(){
var person = new app.models.Profile(app.parsePreload("person"))
person.deferred = $.when(true)
return person
},
findByGuid : function(personId){
var person = new app.models.Profile({ id : personId})
person.fetch()
person.deferred = person.fetch()
return person
}
});

View file

@ -64,6 +64,7 @@ app.models.Stream = Backbone.Collection.extend({
preload : function(){
this.items.reset(app.parsePreload("stream"))
this.deferred = $.when(true)
this.trigger("fetched")
}
});

View file

@ -10,23 +10,25 @@ app.views.Canvas = app.views.InfScroll.extend({
},
renderTemplate : function() {
if(this.stream.items.isEmpty()){
var message
, person = app.page.model
if(person.get("is_own_profile")){
message = "Make something to start the magic."
this.stream.deferred.done(_.bind(function(){
if(this.stream.items.isEmpty()){
var message
, person = app.page.model
if(person.get("is_own_profile")){
message = "Make something to start the magic."
} else {
var name = person.get("name") || ""
message = name + " hasn't posted anything yet."
}
this.$el.html("<p class='no-post-message'>" + message + "</p>")
} else {
var name = person.get("name") || ""
message = name + " hasn't posted anything yet."
this.renderInitialPosts()
}
this.$el.html("<p class='no-post-message'>" + message + "</p>")
} else {
this.renderInitialPosts()
}
//needs to be deferred so it happens after html rendering finishes
_.defer(_.bind(this.mason, this))
//needs to be deferred so it happens after html rendering finishes
_.defer(_.bind(this.mason, this))
}, this))
},
addPostView : function(post) {

View file

@ -2,6 +2,7 @@ describe("app.pages.Profile", function(){
beforeEach(function(){
this.guid = 'abcdefg123'
this.profile = factory.profile({personId: this.guid})
spyOn(app.collections.Posts.prototype, "fetch").andReturn(new $.Deferred)
app.page = this.page = new app.pages.Profile({model : this.profile });
this.stream = this.page.stream
});
@ -34,6 +35,7 @@ describe("app.pages.Profile", function(){
context("with no posts", function(){
beforeEach(function(){
this.profile.set({"name" : "Alice Waters", person_id : "889"})
this.stream.deferred.resolve()
})
it("has a message that there are no posts", function(){
@ -52,6 +54,7 @@ describe("app.pages.Profile", function(){
beforeEach(function(){
this.post = factory.post()
this.stream.add(this.post)
this.stream.deferred.resolve()
this.page.toggleEdit()
expect(this.page.editMode).toBeTruthy()
this.page.render()