Skip to content

Commit c99a5dd

Browse files
committed
fix issue in initial DOM node rendering
1 parent d97287c commit c99a5dd

File tree

4 files changed

+28
-50
lines changed

4 files changed

+28
-50
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,8 @@ canny.whisker.add(node, function (whiskerCb) {
519519
* repeat function pointer can be called with custom scope
520520
* repeat function pointer can be called without any scope (default scope is item)
521521
* remove on-click attribute from README
522-
* mark object {for:..., in:...} as "legacy" and force to use string instead
522+
* mark object {for:..., in:...} as "legacy" and force to use string instead
523+
524+
**0.2.1**
525+
* whisker
526+
* fix issue in initial DOM node rendering

mod/whisker.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
function compileTextNode(node, dataObj, itemName) {
7676
var tokens = parse(node.nodeValue),
7777
obj = dataObj,
78-
el, token, i, l, tmp, tokenObjectProperty, val;
78+
el, token, i, l, tmp, tokenObjectProperty, val, valUnknown;
7979
if (!tokens || obj === undefined || typeof obj === 'string') {return; }
8080

8181
for (i = 0, l = tokens.length; i < l; i++) {
@@ -86,22 +86,29 @@
8686
if (tmp.length > 0 && tmp[0] === itemName) {
8787
tokenObjectProperty = tmp.slice(1).join('.');
8888
if (typeof obj === 'object') {
89-
val = getGlobalCall(tokenObjectProperty, obj);
89+
valUnknown = getGlobalCall(tokenObjectProperty, obj);
9090
} else {
91-
val = obj;
91+
valUnknown = obj;
9292
}
9393
} else {
9494
// just a string?
95-
val = obj;
95+
valUnknown = obj;
96+
}
97+
98+
if (typeof valUnknown === 'function') {
99+
val = valUnknown(node);
100+
} else {
101+
val = valUnknown;
96102
}
103+
97104
if (typeof val === 'string' || typeof val === 'number') {
98105
el = document.createTextNode(val);
99106
node.parentNode.insertBefore(el, node);
100107
} else if (typeof val === 'boolean') {
101108
el = document.createTextNode(val.toString());
102109
node.parentNode.insertBefore(el, node);
103-
} else if (typeof val === 'function') {
104-
el = document.createTextNode(val(node.parentNode));
110+
} else if (val instanceof HTMLElement) {
111+
el = val;
105112
node.parentNode.insertBefore(el, node);
106113
} else if (tmp[0] === itemName) {
107114
// property is not exists but it is the same scope

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"auth": "eightyfour",
44
"email": "[email protected]",
55
"description": "Simple dom module manager with basic view support.",
6-
"version": "0.2.0",
6+
"version": "0.2.1",
77
"browserify": "canny.js",
88
"engines": {
99
"node": ">= v0.10.0"

spec/test.html

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -99,56 +99,23 @@
9999
},
100100
concatStringAttributes: function (fc) {
101101
concatStringAttributesFc = fc;
102+
var div = document.createElement('span');
103+
div.innerHTML = 'Initial node';
104+
div.className = 'bold';
102105
fc('scope', {
103106
// value: "before",
104-
attribute : function (node) {
105-
console.log('test:attribute', node);
106-
return 'HelloAttribute'
107-
},
108-
bind: function (node) {
109-
// node.innerHTML = fcCallIndex;
110-
// if (node.getAttribute('data') === '1')
111-
// return false;
112-
},
113-
innerBind: function (node) {
114-
// node.innerHTML = fcCallIndex;
115-
// if (node.getAttribute('data') === '1')
116-
// return false;
117-
}
107+
attribute : div,
118108
});
119109
},
120110
add: function (node, attr) {
121111
if (attr === 'btn-1') {
122112
node.addEventListener('click', function () {
123113
fcCallIndex++;
114+
var div = document.createElement('span');
115+
div.innerHTML = 'mix node' + fcCallIndex;
116+
div.className = 'bold';
124117
concatStringAttributesFc({
125-
attribute : function (node) {
126-
console.log('test:attribute1', node);
127-
return 'newClassName' + fcCallIndex;
128-
}
129-
// value: "HALLO ",
130-
// mix: function (node) {
131-
// console.log('test:mix', node.parentNode);
132-
// return fcCallIndex % 2 ? 'foo buhhh yahooo' : (function () {
133-
// var div = document.createElement('strong');
134-
// div.innerHTML = 'mix node' + fcCallIndex;
135-
// div.className = 'bold';
136-
// return div;
137-
// }())
138-
// },
139-
// deep : {
140-
// inside : 'hu hu hu',
141-
// insideFc : function (node) {
142-
// console.log('test:insideFc', node);
143-
// return 'function blob'
144-
// }
145-
// },
146-
// tagSample : (function () {
147-
// var div = document.createElement('strong');
148-
// div.innerHTML = '99999' + fcCallIndex;
149-
// div.className = 'bold';
150-
// return div;
151-
// }())
118+
attribute : div
152119
})
153120
})
154121
} else if (attr === 'btn-2') {
@@ -175,7 +142,7 @@
175142

176143
<div id="concatStringAttributes" canny-mod="whisker" canny-var="canny.whiskerSample.concatStringAttributes">
177144
<!--<div class="test {{scope.value}}"></div>-->
178-
<div class="foo {{scope.attribute}}"></div>
145+
<div class="foo">{{scope.attribute}}</div>
179146
<!--<div>deep {{scope.deep.inside}}</div>-->
180147
<!--<div>deepFC {{scope.deep.insideFc}}</div>-->
181148
<!--<div>ADN HERE THE MIX "{{scope.mix}}" :)</div>-->

0 commit comments

Comments
 (0)