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
}
removeFromRelationships() {
removeAllRelationships() {
// must override
}

View File

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

View File

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

View File

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