refactor stream fetching logic

This commit is contained in:
danielgrippi 2012-04-17 19:18:02 -07:00
parent 16223d003f
commit 37cd6f616e
2 changed files with 12 additions and 27 deletions

View file

@ -15,34 +15,21 @@ app.models.Stream = Backbone.Collection.extend({
return _.any(this.posts.models) ? this.timeFilteredPath() : this.basePath()
},
_fetching : false,
fetch: function() {
if(this._fetching) { return false; }
var self = this
// we're fetching the collection... there is probably a better way to do this
self._fetching = true;
this.posts
.fetch({
if(this.deferred && !this.deferred.isResolved()){ return false }
var url = this.url()
this.deferred = this.posts.fetch({
add : true,
url : self.url()
})
.done(
function(resp){
// we're done fetching... there is probably a better way to handle this
self._fetching = false;
self.trigger("fetched", self);
url : url
}).done(_.bind(this.triggerFetchedEvents, this))
},
triggerFetchedEvents : function(resp){
this.trigger("fetched", this);
// all loaded?
if(resp.posts && (resp.posts.author || resp.posts.length == 0)) {
self.trigger("allPostsLoaded", self);
this.trigger("allPostsLoaded", this);
}
}
)
return this;
},
basePath : function(){

View file

@ -9,8 +9,6 @@ describe("app.views.Stream", function() {
this.view = new app.views.Stream({model : this.stream});
app.stream.bind("fetched", this.collectionFetched, this); //untested
// do this manually because we've moved loadMore into render??
this.view.render();
_.each(this.view.collection.models, function(post) {