From 013356b4ae06a90d1ba1736b8272128531a5fc82 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sat, 13 Jan 2018 15:46:38 +0100 Subject: [PATCH] Add `component:remove` event. Closes #748 --- src/dom_components/model/Component.js | 11 +++++++++++ src/editor/index.js | 1 + 2 files changed, 12 insertions(+) diff --git a/src/dom_components/model/Component.js b/src/dom_components/model/Component.js index e21b6b315..c6ae5e544 100644 --- a/src/dom_components/model/Component.js +++ b/src/dom_components/model/Component.js @@ -176,6 +176,7 @@ const Component = Backbone.Model.extend(Styleable).extend( this.config = opt.config || {}; this.ccid = Component.createId(this); this.set('attributes', this.get('attributes') || {}); + this.on('destroy', this.handleRemove); this.listenTo(this, 'change:script', this.scriptUpdated); this.listenTo(this, 'change:traits', this.traitsUpdated); this.listenTo(this, 'change:tagName', this.tagUpdated); @@ -191,6 +192,16 @@ const Component = Backbone.Model.extend(Styleable).extend( this.init(); }, + /** + * Triggered on model remove + * @param {Model} removed Removed model + * @private + */ + handleRemove(removed) { + const em = this.em; + em && em.trigger('component:remove', removed); + }, + /** * Check component's type * @param {string} type Component type diff --git a/src/editor/index.js b/src/editor/index.js index dcb86cccf..685c26f21 100644 --- a/src/editor/index.js +++ b/src/editor/index.js @@ -10,6 +10,7 @@ * * ## Components * * `component:add` - Triggered when a new component is added to the editor, the model is passed as an argument to the callback + * * `component:remove` - Triggered when a component is removed, the model is passed as an argument to the callback * * `component:clone` - Triggered when a new component is added by a clone command, the model is passed as an argument to the callback * * `component:update` - Triggered when a component is updated (moved, styled, etc.), the model is passed as an argument to the callback * * `component:update:{propertyName}` - Listen any property change, the model is passed as an argument to the callback