Refactor the way we insert new records with pusher

Previousy I was using find to ensure that the record is materialized,
but the new version is much lighter - it uses Model#load to load the
record directly
This commit is contained in:
Piotr Sarnacki 2013-08-03 15:09:18 +02:00
parent 9cb84f78ee
commit 66ed172888
2 changed files with 18 additions and 11 deletions

View File

@ -96,22 +96,18 @@ unless window.TravisApplication
_loadOne: (store, type, json) ->
root = type.singularName()
result = @loadOrMerge(type, json[root])
if result && result.id
record = type.find(result.id)
# TODO: find a nicer way to not add record to record arrays twice
if !type._findAllRecordArray || !type._findAllRecordArray.contains(record)
type.addToRecordArrays(record)
reference = @loadOrMerge(type, json[root])
unless reference.record
type.loadRecordForReference(reference)
# we get other types of records only in a few situations and
# it's not always needed to update data, so I'm specyfing which
# things I want to update here:
if type == Travis.Build && (json.repository || json.repo)
result = @loadOrMerge(Travis.Repo, json.repository || json.repo)
if result && result.id
record = Travis.Repo.find(result.id)
if !Travis.Repo._findAllRecordArray || !Travis.Repo._findAllRecordArray.contains(record)
Travis.Repo.addToRecordArrays(record)
data = json.repository || json.repo
reference = @loadOrMerge(Travis.Repo, data)
unless reference.record
Travis.Repo.loadRecordForReference(reference)
loadOrMerge: (type, hash, options) ->
options ||= {}

View File

@ -167,3 +167,14 @@ Array.prototype.diff = (a) ->
delete this.sideloadedData[key]
if @recordCache && @recordCache[key]
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
reference.record = record
record.load(reference.id, this.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)