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.
This commit is contained in:
Piotr Sarnacki 2013-09-04 21:15:54 +02:00
parent 2b63b8ca71
commit 12aaeeef2d

View File

@ -172,12 +172,23 @@ Array.prototype.diff = (a) ->
delete this.recordCache[key] delete this.recordCache[key]
loadRecordForReference: (reference) -> loadRecordForReference: (reference) ->
record = this.create({ _reference: reference }) record = @create({ _reference: reference })
this.recordCache = {} unless this.recordCache @recordCache = {} unless @recordCache
this.sideloadedData = {} unless this.sideloadedData @sideloadedData = {} unless @sideloadedData
this.recordCache[reference.id] = record @recordCache[reference.id] = record
reference.record = 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 # TODO: find a nicer way to not add record to record arrays twice
if !this._findAllRecordArray || !this._findAllRecordArray.contains(record) if @currentRecordsToAdd
this.addToRecordArrays(record) @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