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

View File

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