From 12aaeeef2dd7fe155b99a10e6982b2d536e0839b Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 4 Sep 2013 21:15:54 +0200 Subject: [PATCH] Add records to record arrays in a runloop When adding records just after loading them, the elements in the UI might have to be updated, which may trigger `get` on associations right away. As an effect, even if we load a few records on an event (like repository on build:started event), ember model may end up fetching the record. This commit fixes such occurrences by adding record to record arrays in a run loop, so newly created records will be added at once, after the event was served. --- assets/scripts/lib/travis/model.coffee | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/assets/scripts/lib/travis/model.coffee b/assets/scripts/lib/travis/model.coffee index 914970b6..4c37fe50 100644 --- a/assets/scripts/lib/travis/model.coffee +++ b/assets/scripts/lib/travis/model.coffee @@ -172,12 +172,23 @@ Array.prototype.diff = (a) -> delete this.recordCache[key] loadRecordForReference: (reference) -> - record = this.create({ _reference: reference }) - this.recordCache = {} unless this.recordCache - this.sideloadedData = {} unless this.sideloadedData - this.recordCache[reference.id] = record + record = @create({ _reference: reference }) + @recordCache = {} unless @recordCache + @sideloadedData = {} unless @sideloadedData + @recordCache[reference.id] = record reference.record = record - record.load(reference.id, this.sideloadedData[reference.id]) + record.load(reference.id, @sideloadedData[reference.id]) # TODO: find a nicer way to not add record to record arrays twice - if !this._findAllRecordArray || !this._findAllRecordArray.contains(record) - this.addToRecordArrays(record) + if @currentRecordsToAdd + @currentRecordsToAdd.pushObject(record) unless @currentRecordsToAdd.contains(record) + else + @currentRecordsToAdd = [record] + + Ember.run.scheduleOnce('data', this, @_batchAddToRecordArrays); + + _batchAddToRecordArrays: -> + for record in @currentRecordsToAdd + if !@_findAllRecordArray || !@_findAllRecordArray.contains(record) + @addToRecordArrays(record) + + @currentRecordsToAdd = null