From 66ed17288861738e7cd97dd22bb0ec2d4bcccd09 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sat, 3 Aug 2013 15:09:18 +0200 Subject: [PATCH] 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 --- assets/scripts/app/app.coffee | 18 +++++++----------- assets/scripts/lib/travis/model.coffee | 11 +++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/assets/scripts/app/app.coffee b/assets/scripts/app/app.coffee index 99623ecf..6be3abbc 100644 --- a/assets/scripts/app/app.coffee +++ b/assets/scripts/app/app.coffee @@ -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 ||= {} diff --git a/assets/scripts/lib/travis/model.coffee b/assets/scripts/lib/travis/model.coffee index 99e50175..c81ac4b6 100644 --- a/assets/scripts/lib/travis/model.coffee +++ b/assets/scripts/lib/travis/model.coffee @@ -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)