Skip to content

Commit

Permalink
Init the component with initial values from traits. Closes #230
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Aug 17, 2017
1 parent 9f39cdf commit 562e0e0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 0 additions & 5 deletions index.html
Expand Up @@ -1326,11 +1326,6 @@ <h1 class="heading">Insert title here</h1>
let computedValue = view.getComputedValue();
let defaultValue = view.getDefaultValue();
//console.log('Style of ', model.get('property'), 'Target: ', targetValue, 'Computed:', computedValue, 'Default:', defaultValue);
})

editor.on('block:drag:stop', function(model) {
console.log(model);
model && editor.select(model);
});

editor.render();
Expand Down
18 changes: 18 additions & 0 deletions src/dom_components/model/Component.js
Expand Up @@ -114,6 +114,7 @@ module.exports = Backbone.Model.extend(Styleable).extend({
this.components = new Components(this.defaultC, opt);
this.components.parent = this;
this.listenTo(this, 'change:script', this.scriptUpdated);
this.listenTo(this, 'change:traits', this.traitsUpdated);
this.set('attributes', this.get('attributes') || {});
this.set('components', this.components);
this.set('classes', new Selectors(this.defaultCl));
Expand Down Expand Up @@ -150,6 +151,23 @@ module.exports = Backbone.Model.extend(Styleable).extend({
this.set('scriptUpdated', 1);
},

/**
* Once traits are updated I have to populates model's attributes
*/
traitsUpdated() {
let found = 0;
const attrs = Object.assign({}, this.get('attributes'));

this.get('traits').each((trait) => {
found = 1;
if (!trait.get('changeProp')) {
attrs[trait.get('name')] = trait.getInitValue();
}
});

found && this.set('attributes', attrs);
},

/**
* Init toolbar
*/
Expand Down
4 changes: 4 additions & 0 deletions src/trait_manager/model/Trait.js
Expand Up @@ -23,4 +23,8 @@ module.exports = Backbone.Model.extend({
}
},

getInitValue() {
return this.get('value') || this.get('default');
}

});

0 comments on commit 562e0e0

Please sign in to comment.