From 37cd6f616edc0e20f9b757fb208b5971ab88229e Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Tue, 17 Apr 2012 19:18:02 -0700 Subject: [PATCH] refactor stream fetching logic --- app/assets/javascripts/app/models/stream.js | 37 ++++++------------- .../javascripts/app/views/stream_view_spec.js | 2 - 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/app/assets/javascripts/app/models/stream.js b/app/assets/javascripts/app/models/stream.js index 619499c31..46ac8e5ea 100644 --- a/app/assets/javascripts/app/models/stream.js +++ b/app/assets/javascripts/app/models/stream.js @@ -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; + url : url + }).done(_.bind(this.triggerFetchedEvents, this)) + }, - self.trigger("fetched", self); - - // all loaded? - if(resp.posts && (resp.posts.author || resp.posts.length == 0)) { - self.trigger("allPostsLoaded", self); - } - } - ) - return this; + triggerFetchedEvents : function(resp){ + this.trigger("fetched", this); + // all loaded? + if(resp.posts && (resp.posts.author || resp.posts.length == 0)) { + this.trigger("allPostsLoaded", this); + } }, basePath : function(){ diff --git a/spec/javascripts/app/views/stream_view_spec.js b/spec/javascripts/app/views/stream_view_spec.js index 7f14ae523..ffbac54e5 100644 --- a/spec/javascripts/app/views/stream_view_spec.js +++ b/spec/javascripts/app/views/stream_view_spec.js @@ -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) {