Skip to content

Commit a14fcc2

Browse files
committed
feat: support for value-type attribute to define if render will be a string, text, innerHTML. Defaults to innerHTML
1 parent b9bb90f commit a14fcc2

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

demo/render.array.html

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ <h4>{{domains}}</h4>
1919
</cc-options>
2020

2121
<div class="template" template_id="abc1">
22-
<div class="template" render-array="data.current_org" render-key="current_org" value="{{current_org}}">
23-
<h3>{{current_org}}</h3>
22+
<div class="template" render-array="data.current" render-key="current_org" value="{{current_org.name}}">
23+
<h3>{{current_org.name}}</h3>
2424
</div>
2525
</div>
2626

@@ -34,7 +34,8 @@ <h1>Paste in your browser console</h1>
3434
collection : 'dededede',
3535
data: [{
3636
_id : '123kijfhgfkel45',
37-
current_org: ['test1', 'test2', 'test31', 'test3']
37+
current_org: ['test1', 'test2', 'test31', 'test3'],
38+
current: [{name: 'test1w'}, {name: 'test2'}, {name: 'test31'}, {name: 'test3'}]
3839
}]
3940
}
4041

demo/render.object.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<body>
1212

1313
<div class="template" template_id="abc1">
14-
<div>{{data}}</div>
15-
<h1> {{data.collection}} {{data._id}}</h1>
14+
<!-- <div>{{data}}</div>
15+
<h1> {{data.collection}} {{data._id}}</h1> -->
1616
<div class="template" render-object="" render-key="one">
1717
<h5>{{one.key}}: {{one.value}}</h5>
1818
</div>

src/index.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ const CoCreateRender = {
162162
}
163163
});
164164

165+
// if (el.classList.contains('template')) {
166+
// that.render(el, data);
167+
// }
168+
// if(el.childNodes.length > 0) {
169+
// that.setValue(el.childNodes, data, renderKey);
170+
// }
171+
165172
if(el.childNodes.length > 0) {
166173
that.setValue(el.childNodes, data, renderKey);
167174
}
@@ -171,12 +178,17 @@ const CoCreateRender = {
171178
}
172179

173180
if (el.nodeType == 3) {
181+
let valueType = el.parentElement.getAttribute('value-type')
174182
let textContent = el.textContent;
175183
let text = that.__replaceValue(data, textContent, renderKey);
176184
if (text || text == "") {
177-
const newNode = document.createElement('div');
178-
newNode.innerHTML = text;
179-
el.replaceWith(...newNode.childNodes)
185+
if (valueType == 'text' || valueType == 'string'){
186+
el.textContent = text;
187+
} else {
188+
const newNode = document.createElement('div');
189+
newNode.innerHTML = text;
190+
el.replaceWith(...newNode.childNodes)
191+
}
180192
}
181193
}
182194

0 commit comments

Comments
 (0)