Skip to content

Commit 7743acf

Browse files
authored
fix: don't trigger events due to teardown (#98)
1 parent 7a1c6b4 commit 7743acf

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

addon/components/grid-stack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export default class GridStackComponent extends Component {
159159
el.remove(); // in batch mode engine.removeNode doesn't call back to remove DOM
160160
}
161161
});
162-
if (triggerEvent) {
162+
if (triggerEvent && !this.isDestroying && !this.isDestroyed) {
163163
this.gridStack?._triggerRemoveEvent();
164164
this.gridStack?._triggerChangeEvent();
165165
}

tests/integration/components/grid-stack-test.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ module('Integration | Component | grid stack', function (hooks) {
145145
});
146146

147147
test('onChange, onAdded, onRemove actions', async function (assert) {
148-
// onAdded should run twice, onChange once, onRemoved twice
149-
assert.expect(15);
148+
assert.expect(12);
150149

151150
this.set('items', A([1]));
152151

@@ -303,4 +302,37 @@ module('Integration | Component | grid stack', function (hooks) {
303302
.dom(`[data-id="3"]`)
304303
.hasAttribute('gs-y', '2', 'Updating a grid-stack-item moves conflicting items to a different row');
305304
});
305+
306+
test('do not fire `onChange` or `onRemoved` during teardown', async function (assert) {
307+
assert.expect(1);
308+
309+
this.set('shouldRender', true);
310+
this.set('items', [
311+
{ id: 0, options: { x: 0, y: 0, w: 5, h: 1 } },
312+
{ id: 1, options: { x: 6, y: 0, w: 3, h: 1 } },
313+
{ id: 2, options: { x: 0, y: 1, w: 4, h: 1 } },
314+
{ id: 3, options: { x: 6, y: 1, w: 2, h: 1 } },
315+
]);
316+
317+
this.onChange = () => assert.notOk(true, '`onChange` should not fire on teardown');
318+
this.onRemoved = () => assert.notOk(true, '`onRemoved` should not fire on teardown');
319+
320+
await render(hbs`
321+
{{#if this.shouldRender}}
322+
<GridStack
323+
onChange={{this.onChange}}
324+
onRemoved={{this.onChange}}
325+
>
326+
{{#each this.items as |item|}}
327+
<GridStackItem data-id={{item.id}} @options={{item.options}}>
328+
{{item.id}}
329+
</GridStackItem>
330+
{{/each}}
331+
</GridStack>
332+
{{/if}}
333+
`);
334+
335+
this.set('shouldRender', false);
336+
assert.ok(true, 'all is good');
337+
});
306338
});

0 commit comments

Comments
 (0)