From dcb2945092c71fc6ba0b36918fe914eb8c4856a6 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 5 Aug 2013 13:12:31 +0200 Subject: [PATCH] Buffer pusher updates to RecordArrays until content is set When collections are loading as ajax requests, we may still get pusher updates, which will try to add records to these collections. In order to make it work nicely, we should wait until Ajax request finishes (ie. content of record array is set) and only then add objects from pusher --- assets/scripts/travis.coffee | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/assets/scripts/travis.coffee b/assets/scripts/travis.coffee index 137ab185..6d965e78 100644 --- a/assets/scripts/travis.coffee +++ b/assets/scripts/travis.coffee @@ -32,6 +32,34 @@ Storage = Em.Object.extend clear: -> @set('storage', {}) +Ember.RecordArray.reopen + # TODO: ember.js changed a way ArrayProxies behave, so that check for content is done + # in _replace method. I should not be overriding it, because it's private, but + # there is no easy other way to do it at this point + _replace: (index, removedCount, records) -> + # in Travis it's sometimes the case that we add new records to RecordArrays + # from pusher before its content has loaded from an ajax query. In order to handle + # this case nicer I'm extending record array to buffer those records and push them + # to content when it's available + @bufferedRecords = [] unless @bufferedRecords + + if !@get('content') + for record in records + @bufferedRecords.pushObject(record) unless @bufferedRecords.contains(record) + + records = [] + + # call super only if there's anything more to add + if removedCount || records.length + @_super(index, removedCount, records) + + contentDidChange: (-> + if (content = @get('content')) && @bufferedRecords && @bufferedRecords.length + for record in @bufferedRecords + content.pushObject(record) unless content.contains(record) + @bufferedRecords = [] + ).observes('content') + window.Travis = TravisApplication.create( LOG_TRANSITIONS: true )