Skip to content

Commit

Permalink
fix(lib): fix teardown of lib objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Metzener authored and anehx committed Jul 30, 2019
1 parent 9272cf1 commit bd583a2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
8 changes: 5 additions & 3 deletions addon/components/cf-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export default Component.extend(ComponentQueryManager, {
willDestroy() {
this._super(...arguments);

this.calumaStore.clear();
this.document.destroy();

if (this.navigation) {
this.navigation.destroy();
}
},

/**
Expand Down Expand Up @@ -152,8 +156,6 @@ export default Component.extend(ComponentQueryManager, {
"allForms.edges"
)).map(({ node }) => node);

this.calumaStore.clear();

return Document.create(getOwner(this).ownerInjection(), {
raw: parseDocument({ ...answerDocument, form })
});
Expand Down
10 changes: 9 additions & 1 deletion addon/lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default Base.extend({

_createRootForm() {
const rootForm =
this.calumaStore.find(`Form:${this.raw.rootForm}`) ||
this.calumaStore.find(`Form:${this.raw.rootForm.slug}`) ||
Form.create(getOwner(this).ownerInjection(), {
raw: this.raw.rootForm
});
Expand All @@ -60,6 +60,14 @@ export default Base.extend({
this.set("fieldsets", fieldsets);
},

willDestroy() {
this._super(...arguments);

const fieldsets = this.fieldsets;
this.set("fieldsets", []);
fieldsets.forEach(fieldset => fieldset.destroy());
},

/**
* The uuid of the document
*
Expand Down
8 changes: 8 additions & 0 deletions addon/lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ export default Base.extend(ObjectQueryManager, {
this.set("_errors", []);
},

willDestroy() {
this._super(...arguments);

if (this.answer) {
this.answer.destroy();
}
},

_createQuestion() {
const question =
this.calumaStore.find(`Question:${this.raw.question.slug}`) ||
Expand Down
8 changes: 8 additions & 0 deletions addon/lib/fieldset.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export default Base.extend({
this._createFields();
},

willDestroy() {
this._super(...arguments);

const fields = this.fields;
this.set("fields", []);
fields.forEach(field => field.destroy());
},

_createForm() {
const form =
this.calumaStore.find(`Form:${this.raw.form.slug}`) ||
Expand Down
24 changes: 15 additions & 9 deletions addon/lib/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ export const NavigationItem = Base.extend({

init() {
assert("A fieldset `fieldset` must be passed", this.fieldset);
assert("A navigation `navigation` must be passed", this.navigation);

defineProperty(this, "pk", {
writable: false,
value: `NavigationItem:${this.fieldset.pk}`
});

this._super(...arguments);

this.set("_items", []);
},

/**
Expand All @@ -33,8 +32,8 @@ export const NavigationItem = Base.extend({
* @property {NavigationItem} parent
* @accessor
*/
parent: computed("_parentSlug", "_items.@each.slug", function() {
return this._items.find(item => item.slug === this._parentSlug);
parent: computed("_parentSlug", "navigation.items.@each.slug", function() {
return this.navigation.items.find(item => item.slug === this._parentSlug);
}),

/**
Expand All @@ -43,8 +42,8 @@ export const NavigationItem = Base.extend({
* @property {NavigationItem[]} children
* @accessor
*/
children: computed("slug", "_items.@each._parentSlug", function() {
return this._items.filter(item => item._parentSlug === this.slug);
children: computed("slug", "navigation.items.@each._parentSlug", function() {
return this.navigation.items.filter(item => item._parentSlug === this.slug);
}),

/**
Expand Down Expand Up @@ -219,6 +218,14 @@ export const Navigation = Base.extend({
this._createItems();
},

willDestroy() {
this._super(...arguments);

const items = this.items;
this.set("items", []);
items.forEach(item => item.destroy());
},

_createItems() {
const items = this.document.fieldsets
.filter(fieldset => fieldset.field)
Expand All @@ -228,13 +235,12 @@ export const Navigation = Base.extend({
return (
this.calumaStore.find(pk) ||
NavigationItem.create(getOwner(this).ownerInjection(), {
fieldset
fieldset,
navigation: this
})
);
});

items.forEach(item => item.set("_items", items));

this.set("items", items);
},

Expand Down

0 comments on commit bd583a2

Please sign in to comment.