Skip to content

Commit

Permalink
Add resetId to the Component model
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Jun 17, 2018
1 parent 816b39f commit 2b241d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/block_manager/view/BlockView.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ module.exports = Backbone.View.extend({
}

if (model.get('resetId')) {
result.onAll(model => {
console.log(model, model.get);
});
result.onAll(model => model.resetId());
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/dom_components/model/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,18 @@ const Component = Backbone.Model.extend(Styleable).extend(
return attrs.id || this.ccid || this.cid;
},

/**
* Return model id
* @param {String} id
* @return {self}
*/
setId(id) {
const attrs = { ...this.get('attributes') };
attrs.id = id;
this.set('attributes', attrs);
return this;
},

/**
* Get the DOM element of the model. This works only of the
* model is alredy rendered
Expand Down Expand Up @@ -854,6 +866,23 @@ const Component = Backbone.Model.extend(Styleable).extend(
this.components().forEach(model => model.onAll(clb));
}
return this;
},

/**
* Reset id of the component and any of its style rule
* @param {Object} [opts={}] Options
* @return {self}
*/
resetId(opts = {}) {
const { em } = this;
const oldId = this.getId();
if (!oldId) return;
const newId = Component.createId(this);
this.setId(newId);
const rule = em && em.get('CssComposer').getIdRule(oldId);
const selector = rule && rule.get('selectors').at(0);
selector && selector.set('name', newId);
return this;
}
},
{
Expand Down

0 comments on commit 2b241d6

Please sign in to comment.