Browse Source

Add the possibility to create custom attributes in `toHTML` method

pull/1229/head
Artur Arseniev 8 years ago
parent
commit
d82cdc55e8
  1. 18
      src/dom_components/model/Component.js

18
src/dom_components/model/Component.js

@ -1,5 +1,7 @@
import { import {
isUndefined, isUndefined,
isFunction,
isObject,
isArray, isArray,
isEmpty, isEmpty,
isBoolean, isBoolean,
@ -686,13 +688,23 @@ const Component = Backbone.Model.extend(Styleable).extend(
* @return {string} HTML string * @return {string} HTML string
* @private * @private
*/ */
toHTML(opts) { toHTML(opts = {}) {
const model = this; const model = this;
const attrs = []; const attrs = [];
const classes = []; const classes = [];
const tag = model.get('tagName'); const tag = model.get('tagName');
const sTag = model.get('void'); const sTag = model.get('void');
const attributes = this.getAttrToHTML(); const customAttr = opts.attributes;
let attributes = this.getAttrToHTML();
// Get custom attributes if requested
if (customAttr) {
if (isFunction(customAttr)) {
attributes = customAttr(model, attributes) || {};
} else if (isObject(customAttr)) {
attributes = customAttr;
}
}
for (let attr in attributes) { for (let attr in attributes) {
const val = attributes[attr]; const val = attributes[attr];
@ -711,7 +723,7 @@ const Component = Backbone.Model.extend(Styleable).extend(
let code = `<${tag}${attrString}${sTag ? '/' : ''}>${model.get( let code = `<${tag}${attrString}${sTag ? '/' : ''}>${model.get(
'content' 'content'
)}`; )}`;
model.get('components').each(comp => (code += comp.toHTML())); model.get('components').each(comp => (code += comp.toHTML(opts)));
!sTag && (code += `</${tag}>`); !sTag && (code += `</${tag}>`);
return code; return code;

Loading…
Cancel
Save