Browse Source

Add new hook methods to Component, updated and removed

improve-component-api
Artur Arseniev 7 years ago
parent
commit
cd6b07a983
  1. 32
      src/dom_components/model/Component.js
  2. 2
      src/dom_components/view/ComponentsView.js
  3. 2
      src/editor/index.js

32
src/dom_components/model/Component.js

@ -118,6 +118,24 @@ const Component = Backbone.Model.extend(Styleable).extend(
toolbar: null toolbar: null
}, },
/**
* Hook method, called once the model is created
*/
init() {},
/**
* Hook method, called when the model has been updated (eg. updated some model's property)
* @param {String} property Property name, if triggered after some property update
* @param {*} value Property value, if triggered after some property update
* @param {*} previous Property previous value, if triggered after some property update
*/
updated(property, value, previous) {},
/**
* Hook method, called once the model has been removed
*/
removed() {},
initialize(props = {}, opt = {}) { initialize(props = {}, opt = {}) {
const em = opt.em; const em = opt.em;
@ -170,10 +188,7 @@ const Component = Backbone.Model.extend(Styleable).extend(
); );
}); });
this.init(); this.init();
em && em.trigger('component:create', this);
if (em) {
em.trigger('component:create', this);
}
}, },
/** /**
@ -508,8 +523,6 @@ const Component = Backbone.Model.extend(Styleable).extend(
return this; return this;
}, },
init() {},
/** /**
* Add new component children * Add new component children
* @param {Component|String} components Component to add * @param {Component|String} components Component to add
@ -941,6 +954,13 @@ const Component = Backbone.Model.extend(Styleable).extend(
emitUpdate(property, ...args) { emitUpdate(property, ...args) {
const em = this.em; const em = this.em;
const event = 'component:update' + (property ? `:${property}` : ''); const event = 'component:update' + (property ? `:${property}` : '');
this.updated(
property,
property && this.get(property),
property && this.previous(property),
...args
);
this.trigger(event, ...args);
em && em.trigger(event, this, ...args); em && em.trigger(event, this, ...args);
}, },

2
src/dom_components/view/ComponentsView.js

@ -18,6 +18,8 @@ module.exports = Backbone.View.extend({
view.remove.apply(view); view.remove.apply(view);
const children = view.childrenView; const children = view.childrenView;
children && children.stopListening(); children && children.stopListening();
removed.components().forEach(this.removeChildren.bind(this));
removed.removed();
if (em) { if (em) {
removed.get('style-signature') && removed.get('style-signature') &&
em em

2
src/editor/index.js

@ -18,7 +18,7 @@
* ``` * ```
* *
* ### Components * ### Components
* * `component:create` - Component is created (only the model, is not yet mounted in the canvas) * * `component:create` - Component is created (only the model, is not yet mounted in the canvas), called after the init() method
* * `component:mount` - Component is monted to an element and rendered in canvas * * `component:mount` - Component is monted to an element and rendered in canvas
* * `component:add` - Triggered when a new component is added to the editor, the model is passed as an argument to the callback * * `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:remove` - Triggered when a component is removed, the model is passed as an argument to the callback

Loading…
Cancel
Save