Skip to content

Commit 938167f

Browse files
authored
Merge pull request #115 from canjs/minor
can-view-live 4.2
2 parents 2d8abd4 + f9368f1 commit 938167f

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

lib/html.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ var nodeLists = require('can-view-nodelist');
44
var makeFrag = require('can-fragment');
55
var childNodes = require('can-child-nodes');
66
var canReflect = require('can-reflect');
7+
var canSymbol = require("can-symbol");
78
var queues = require("can-queues");
9+
var viewInsertSymbol = canSymbol.for("can.viewInsert");
810

911

1012
function updateNodeList(oldNodes, nodes, frag, nodeListUpdatedByFn) {
@@ -48,10 +50,22 @@ function updateNodeList(oldNodes, nodes, frag, nodeListUpdatedByFn) {
4850
*
4951
*
5052
*/
51-
live.html = function(el, compute, parentNode, nodeList) {
52-
var data,
53-
makeAndPut,
54-
nodes;
53+
live.html = function(el, compute, parentNode, nodeListOrOptions) {
54+
var data;
55+
var makeAndPut;
56+
var nodeList;
57+
var nodes;
58+
var options;
59+
60+
// nodeListOrOptions can either be a NodeList or an object with a nodeList property
61+
if (nodeListOrOptions !== undefined) {
62+
if (Array.isArray(nodeListOrOptions)) {
63+
nodeList = nodeListOrOptions;
64+
} else {
65+
nodeList = nodeListOrOptions.nodeList;
66+
options = nodeListOrOptions;
67+
}
68+
}
5569

5670
var meta = {reasonLog: "live.html replace::"+canReflect.getName(compute)};
5771
// prefer to manipulate el's actual parent over the supplied parent
@@ -79,7 +93,7 @@ live.html = function(el, compute, parentNode, nodeList) {
7993
};
8094
}
8195
});
82-
96+
8397
Object.defineProperty(liveHTMLUpdateHTML, "name", {
8498
value: "live.html update::"+canReflect.getName(compute),
8599
});
@@ -93,10 +107,18 @@ live.html = function(el, compute, parentNode, nodeList) {
93107
nodes = nodeList || [el];
94108
makeAndPut = function(val, useQueue) {
95109
// ##### makeandput
96-
// Receives the compute output (must be some DOM representation or a function)
97-
var isFunction = typeof val === "function",
98-
// translate val into a document fragment if it's DOM-like
99-
frag = makeFrag(isFunction ? "" : val);
110+
// Receives the compute output (must be some DOM representation, a function,
111+
// or an object with the can.viewInsert symbol)
112+
113+
// If val has the can.viewInsert symbol, call it and get something usable for val back
114+
if (val && typeof val[viewInsertSymbol] === "function") {
115+
val = val[viewInsertSymbol](options);
116+
}
117+
118+
var isFunction = typeof val === "function";
119+
120+
// translate val into a document fragment if it's DOM-like
121+
var frag = makeFrag(isFunction ? "" : val);
100122

101123
// Add a placeholder textNode if necessary.
102124
live.addTextNodeIfNoChildren(frag);

test/html-test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var NodeLists = require("can-view-nodelist");
77
var testHelpers = require('can-test-helpers');
88
var domMutate = require('can-dom-mutate');
99
var canReflectDeps = require('can-reflect-dependencies');
10+
var canSymbol = require('can-symbol');
1011
var fragment = require("can-fragment");
1112
var queues = require("can-queues");
1213

@@ -84,6 +85,26 @@ QUnit.test("Works with Observations - .html", function(){
8485
equal(div.getElementsByTagName('label').length, 3);
8586
});
8687

88+
test("html live binding handles objects with can.viewInsert symbol", 2, function(assert) {
89+
var div = document.createElement("div");
90+
var options = {};
91+
var placeholder = document.createTextNode("Placeholder text");
92+
div.appendChild(placeholder);
93+
94+
var html = new Observation(function() {
95+
return {
96+
[canSymbol.for("can.viewInsert")]: function() {
97+
assert.equal(arguments[0], options, "options were passed to symbol function");
98+
return document.createTextNode("Replaced text");
99+
}
100+
};
101+
});
102+
103+
live.html(placeholder, html, div, options);
104+
105+
assert.equal(div.textContent, "Replaced text", "symbol function called");
106+
});
107+
87108
testHelpers.dev.devOnlyTest("child elements must disconnect before parents can re-evaluate", 1,function(){
88109
var observable = new SimpleObservable("value");
89110

0 commit comments

Comments
 (0)