modelmanager refactor complete

This commit is contained in:
Mo Bitar
2017-01-05 10:59:44 -06:00
parent 4a35e78765
commit 117d6fca4e
7 changed files with 89 additions and 87 deletions

View File

@@ -68,7 +68,7 @@ class Item {
// must override // must override
} }
removeFromRelationships() { removeAllRelationships() {
// must override // must override
} }

View File

@@ -18,6 +18,7 @@ class Note extends Item {
var references = _.map(this.tags, function(tag){ var references = _.map(this.tags, function(tag){
return {uuid: tag.uuid, content_type: tag.content_type}; return {uuid: tag.uuid, content_type: tag.content_type};
}) })
return references; return references;
} }
@@ -47,11 +48,12 @@ class Note extends Item {
super.removeItemAsRelationship(item); super.removeItemAsRelationship(item);
} }
removeFromRelationships() { removeAllRelationships() {
this.tags.forEach(function(tag){ this.tags.forEach(function(tag){
_.pull(tag.notes, this); _.pull(tag.notes, this);
tag.dirty = true; tag.dirty = true;
}) }.bind(this))
this.tags = [];
} }
static filterDummyNotes(notes) { static filterDummyNotes(notes) {

View File

@@ -17,6 +17,7 @@ class Tag extends Item {
var references = _.map(this.notes, function(note){ var references = _.map(this.notes, function(note){
return {uuid: note.uuid, content_type: note.content_type}; return {uuid: note.uuid, content_type: note.content_type};
}) })
return references; return references;
} }
@@ -45,11 +46,13 @@ class Tag extends Item {
super.removeItemAsRelationship(item); super.removeItemAsRelationship(item);
} }
removeFromRelationships() { removeAllRelationships() {
this.notes.forEach(function(note){ this.notes.forEach(function(note){
_.pull(note.tags, this); _.pull(note.tags, this);
note.dirty = true; note.dirty = true;
}) }.bind(this))
this.notes = [];
} }
get content_type() { get content_type() {

View File

@@ -35,10 +35,16 @@ class ModelManager {
item.updateFromJSON(json_obj); item.updateFromJSON(json_obj);
} }
this.addItem(item);
if(json_obj.content) {
this.resolveReferencesForItem(item)
}
models.push(item) models.push(item)
} }
this.addItems(models)
this.resolveReferences() this.sortItems();
return models; return models;
} }
@@ -66,7 +72,6 @@ class ModelManager {
} }
} }
}.bind(this)) }.bind(this))
} }
addItem(item) { addItem(item) {
@@ -79,27 +84,28 @@ class ModelManager {
}); });
} }
resolveReferences() { resolveReferencesForItem(item) {
for(var item of this.items) {
var contentObject = item.contentObject;
if(!contentObject.references) {
continue;
}
for(var reference of contentObject.references) { var contentObject = item.contentObject;
var referencedItem = this.findItem(reference.uuid); if(!contentObject.references) {
if(referencedItem) { return;
item.addItemAsRelationship(referencedItem); }
} else {
console.log("Unable to find item:", reference.uuid); for(var reference of contentObject.references) {
} var referencedItem = this.findItem(reference.uuid);
if(referencedItem) {
item.addItemAsRelationship(referencedItem);
referencedItem.addItemAsRelationship(item);
} else {
console.log("Unable to find item:", reference.uuid);
} }
} }
this.notes.push.apply(this.notes, _.difference(this.itemsForContentType("Note"), this.notes)); }
sortItems() {
Item.sortItemsByDate(this.notes); Item.sortItemsByDate(this.notes);
this.tags.push.apply(this.tags, _.difference(this.itemsForContentType("Tag"), this.tags));
this.tags.forEach(function(tag){ this.tags.forEach(function(tag){
Item.sortItemsByDate(tag.notes); Item.sortItemsByDate(tag.notes);
}) })
@@ -140,7 +146,7 @@ class ModelManager {
setItemToBeDeleted(item) { setItemToBeDeleted(item) {
item.deleted = true; item.deleted = true;
item.dirty = true; item.dirty = true;
item.removeFromRelationships(); item.removeAllRelationships();
} }
removeItemLocally(item) { removeItemLocally(item) {

View File

@@ -128,6 +128,7 @@
text-align: right; text-align: right;
text-overflow: ellipsis; text-overflow: ellipsis;
overflow: hidden; overflow: hidden;
color: black;
.url { .url {
text-align: right; text-align: right;

View File

@@ -1369,8 +1369,8 @@ var Item = function () {
// must override // must override
} }
}, { }, {
key: 'removeFromRelationships', key: 'removeAllRelationships',
value: function removeFromRelationships() { value: function removeAllRelationships() {
// must override // must override
} }
}, { }, {
@@ -1493,6 +1493,7 @@ var Note = function (_Item2) {
var references = _.map(this.tags, function (tag) { var references = _.map(this.tags, function (tag) {
return { uuid: tag.uuid, content_type: tag.content_type }; return { uuid: tag.uuid, content_type: tag.content_type };
}); });
return references; return references;
} }
}, { }, {
@@ -1525,12 +1526,13 @@ var Note = function (_Item2) {
_get(Note.prototype.__proto__ || Object.getPrototypeOf(Note.prototype), 'removeItemAsRelationship', this).call(this, item); _get(Note.prototype.__proto__ || Object.getPrototypeOf(Note.prototype), 'removeItemAsRelationship', this).call(this, item);
} }
}, { }, {
key: 'removeFromRelationships', key: 'removeAllRelationships',
value: function removeFromRelationships() { value: function removeAllRelationships() {
this.tags.forEach(function (tag) { this.tags.forEach(function (tag) {
_.pull(tag.notes, this); _.pull(tag.notes, this);
tag.dirty = true; tag.dirty = true;
}); }.bind(this));
this.tags = [];
} }
}, { }, {
key: 'referencesAffectedBySharingChange', key: 'referencesAffectedBySharingChange',
@@ -1639,6 +1641,7 @@ var Tag = function (_Item3) {
var references = _.map(this.notes, function (note) { var references = _.map(this.notes, function (note) {
return { uuid: note.uuid, content_type: note.content_type }; return { uuid: note.uuid, content_type: note.content_type };
}); });
return references; return references;
} }
}, { }, {
@@ -1670,12 +1673,14 @@ var Tag = function (_Item3) {
_get(Tag.prototype.__proto__ || Object.getPrototypeOf(Tag.prototype), 'removeItemAsRelationship', this).call(this, item); _get(Tag.prototype.__proto__ || Object.getPrototypeOf(Tag.prototype), 'removeItemAsRelationship', this).call(this, item);
} }
}, { }, {
key: 'removeFromRelationships', key: 'removeAllRelationships',
value: function removeFromRelationships() { value: function removeAllRelationships() {
this.notes.forEach(function (note) { this.notes.forEach(function (note) {
_.pull(note.tags, this); _.pull(note.tags, this);
note.dirty = true; note.dirty = true;
}); }.bind(this));
this.notes = [];
} }
}, { }, {
key: 'referencesAffectedBySharingChange', key: 'referencesAffectedBySharingChange',
@@ -2801,6 +2806,12 @@ var ModelManager = function () {
item.updateFromJSON(json_obj); item.updateFromJSON(json_obj);
} }
this.addItem(item);
if (json_obj.content) {
this.resolveReferencesForItem(item);
}
models.push(item); models.push(item);
} }
} catch (err) { } catch (err) {
@@ -2818,8 +2829,7 @@ var ModelManager = function () {
} }
} }
this.addItems(models); this.sortItems();
this.resolveReferences();
return models; return models;
} }
}, { }, {
@@ -2863,49 +2873,28 @@ var ModelManager = function () {
}); });
} }
}, { }, {
key: 'resolveReferences', key: 'resolveReferencesForItem',
value: function resolveReferences() { value: function resolveReferencesForItem(item) {
var contentObject = item.contentObject;
if (!contentObject.references) {
return;
}
var _iteratorNormalCompletion5 = true; var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false; var _didIteratorError5 = false;
var _iteratorError5 = undefined; var _iteratorError5 = undefined;
try { try {
for (var _iterator5 = this.items[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) { for (var _iterator5 = contentObject.references[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var item = _step5.value; var reference = _step5.value;
var contentObject = item.contentObject; var referencedItem = this.findItem(reference.uuid);
if (!contentObject.references) { if (referencedItem) {
continue; item.addItemAsRelationship(referencedItem);
} referencedItem.addItemAsRelationship(item);
} else {
var _iteratorNormalCompletion6 = true; console.log("Unable to find item:", reference.uuid);
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator6 = contentObject.references[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var reference = _step6.value;
var referencedItem = this.findItem(reference.uuid);
if (referencedItem) {
item.addItemAsRelationship(referencedItem);
} else {
console.log("Unable to find item:", reference.uuid);
}
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
}
}
} }
} }
} catch (err) { } catch (err) {
@@ -2922,11 +2911,12 @@ var ModelManager = function () {
} }
} }
} }
}
this.notes.push.apply(this.notes, _.difference(this.itemsForContentType("Note"), this.notes)); }, {
key: 'sortItems',
value: function sortItems() {
Item.sortItemsByDate(this.notes); Item.sortItemsByDate(this.notes);
this.tags.push.apply(this.tags, _.difference(this.itemsForContentType("Tag"), this.tags));
this.tags.forEach(function (tag) { this.tags.forEach(function (tag) {
Item.sortItemsByDate(tag.notes); Item.sortItemsByDate(tag.notes);
}); });
@@ -2944,13 +2934,13 @@ var ModelManager = function () {
}, { }, {
key: 'notifyObserversOfSyncCompletion', key: 'notifyObserversOfSyncCompletion',
value: function notifyObserversOfSyncCompletion() { value: function notifyObserversOfSyncCompletion() {
var _iteratorNormalCompletion7 = true; var _iteratorNormalCompletion6 = true;
var _didIteratorError7 = false; var _didIteratorError6 = false;
var _iteratorError7 = undefined; var _iteratorError6 = undefined;
try { try {
for (var _iterator7 = this.changeObservers[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) { for (var _iterator6 = this.changeObservers[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var observer = _step7.value; var observer = _step6.value;
var changedItems = this.dirtyItems.filter(function (item) { var changedItems = this.dirtyItems.filter(function (item) {
return item.content_type == observer.type; return item.content_type == observer.type;
@@ -2959,16 +2949,16 @@ var ModelManager = function () {
observer.callback(changedItems); observer.callback(changedItems);
} }
} catch (err) { } catch (err) {
_didIteratorError7 = true; _didIteratorError6 = true;
_iteratorError7 = err; _iteratorError6 = err;
} finally { } finally {
try { try {
if (!_iteratorNormalCompletion7 && _iterator7.return) { if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator7.return(); _iterator6.return();
} }
} finally { } finally {
if (_didIteratorError7) { if (_didIteratorError6) {
throw _iteratorError7; throw _iteratorError6;
} }
} }
} }
@@ -2994,7 +2984,7 @@ var ModelManager = function () {
value: function setItemToBeDeleted(item) { value: function setItemToBeDeleted(item) {
item.deleted = true; item.deleted = true;
item.dirty = true; item.dirty = true;
item.removeFromRelationships(); item.removeAllRelationships();
} }
}, { }, {
key: 'removeItemLocally', key: 'removeItemLocally',

File diff suppressed because one or more lines are too long