From 63f75491a85ba167fe3300c6fab3908631778fed Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Wed, 27 Sep 2017 13:16:00 -0500 Subject: [PATCH] Model manager and sync updates --- app/assets/javascripts/app/frontend/models/app/note.js | 2 +- app/assets/javascripts/app/frontend/models/app/tag.js | 2 +- app/assets/javascripts/app/services/modelManager.js | 8 +++++--- app/assets/javascripts/app/services/syncManager.js | 8 ++++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/app/frontend/models/app/note.js b/app/assets/javascripts/app/frontend/models/app/note.js index db8f69a9c..8c125587c 100644 --- a/app/assets/javascripts/app/frontend/models/app/note.js +++ b/app/assets/javascripts/app/frontend/models/app/note.js @@ -60,7 +60,7 @@ class Note extends Item { super.removeReferencesNotPresentIn(references); var uuids = references.map(function(ref){return ref.uuid}); - this.tags.forEach(function(tag){ + this.tags.slice().forEach(function(tag){ if(!uuids.includes(tag.uuid)) { _.pull(tag.notes, this); _.pull(this.tags, tag); diff --git a/app/assets/javascripts/app/frontend/models/app/tag.js b/app/assets/javascripts/app/frontend/models/app/tag.js index 11cdcf6a0..b3552edb1 100644 --- a/app/assets/javascripts/app/frontend/models/app/tag.js +++ b/app/assets/javascripts/app/frontend/models/app/tag.js @@ -57,7 +57,7 @@ class Tag extends Item { removeReferencesNotPresentIn(references) { var uuids = references.map(function(ref){return ref.uuid}); - this.notes.forEach(function(note){ + this.notes.slice().forEach(function(note){ if(!uuids.includes(note.uuid)) { _.pull(note.tags, this); _.pull(this.notes, note); diff --git a/app/assets/javascripts/app/services/modelManager.js b/app/assets/javascripts/app/services/modelManager.js index c13272d64..a5b5ff682 100644 --- a/app/assets/javascripts/app/services/modelManager.js +++ b/app/assets/javascripts/app/services/modelManager.js @@ -165,8 +165,6 @@ class ModelManager { } addItems(items) { - this.items = _.uniq(this.items.concat(items)); - items.forEach(function(item){ if(item.content_type == "Tag") { if(!_.find(this.tags, {uuid: item.uuid})) { @@ -184,6 +182,10 @@ class ModelManager { this._extensions.unshift(item); } } + + if(!_.find(this.items, {uuid: item.uuid})) { + this.items.push(item); + } }.bind(this)); } @@ -203,7 +205,7 @@ class ModelManager { // If another client removes an item's references, this client won't pick up the removal unless // we remove everything not present in the current list of references - item.removeReferencesNotPresentIn(contentObject.references); + item.removeReferencesNotPresentIn(contentObject.references || []); if(!contentObject.references) { return; diff --git a/app/assets/javascripts/app/services/syncManager.js b/app/assets/javascripts/app/services/syncManager.js index 396a06825..38970ac9a 100644 --- a/app/assets/javascripts/app/services/syncManager.js +++ b/app/assets/javascripts/app/services/syncManager.js @@ -24,8 +24,9 @@ class SyncManager { } writeItemsToLocalStorage(items, offlineOnly, callback) { + var version = this.authManager.protocolVersion(); var params = items.map(function(item) { - var itemParams = new ItemParams(item, null, this.authManager.protocolVersion()); + var itemParams = new ItemParams(item, null, version); itemParams = itemParams.paramsForLocalStorage(); if(offlineOnly) { delete itemParams.dirty; @@ -194,10 +195,13 @@ class SyncManager { this.allRetreivedItems = []; } + var version = this.authManager.protocolVersion(); + var keys = this.authManager.keys(); + var params = {}; params.limit = 150; params.items = _.map(subItems, function(item){ - var itemParams = new ItemParams(item, this.authManager.keys(), this.authManager.protocolVersion()); + var itemParams = new ItemParams(item, keys, version); itemParams.additionalFields = options.additionalFields; return itemParams.paramsForSync(); }.bind(this));