Skip to content

Commit

Permalink
Merge pull request #115 from canjs/minor
Browse files Browse the repository at this point in the history
can-view-live 4.2
  • Loading branch information
chasenlehara authored Jun 29, 2018
2 parents 2d8abd4 + f9368f1 commit 938167f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
40 changes: 31 additions & 9 deletions lib/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var nodeLists = require('can-view-nodelist');
var makeFrag = require('can-fragment');
var childNodes = require('can-child-nodes');
var canReflect = require('can-reflect');
var canSymbol = require("can-symbol");
var queues = require("can-queues");
var viewInsertSymbol = canSymbol.for("can.viewInsert");


function updateNodeList(oldNodes, nodes, frag, nodeListUpdatedByFn) {
Expand Down Expand Up @@ -48,10 +50,22 @@ function updateNodeList(oldNodes, nodes, frag, nodeListUpdatedByFn) {
*
*
*/
live.html = function(el, compute, parentNode, nodeList) {
var data,
makeAndPut,
nodes;
live.html = function(el, compute, parentNode, nodeListOrOptions) {
var data;
var makeAndPut;
var nodeList;
var nodes;
var options;

// nodeListOrOptions can either be a NodeList or an object with a nodeList property
if (nodeListOrOptions !== undefined) {
if (Array.isArray(nodeListOrOptions)) {
nodeList = nodeListOrOptions;
} else {
nodeList = nodeListOrOptions.nodeList;
options = nodeListOrOptions;
}
}

var meta = {reasonLog: "live.html replace::"+canReflect.getName(compute)};
// prefer to manipulate el's actual parent over the supplied parent
Expand Down Expand Up @@ -79,7 +93,7 @@ live.html = function(el, compute, parentNode, nodeList) {
};
}
});

Object.defineProperty(liveHTMLUpdateHTML, "name", {
value: "live.html update::"+canReflect.getName(compute),
});
Expand All @@ -93,10 +107,18 @@ live.html = function(el, compute, parentNode, nodeList) {
nodes = nodeList || [el];
makeAndPut = function(val, useQueue) {
// ##### makeandput
// Receives the compute output (must be some DOM representation or a function)
var isFunction = typeof val === "function",
// translate val into a document fragment if it's DOM-like
frag = makeFrag(isFunction ? "" : val);
// Receives the compute output (must be some DOM representation, a function,
// or an object with the can.viewInsert symbol)

// If val has the can.viewInsert symbol, call it and get something usable for val back
if (val && typeof val[viewInsertSymbol] === "function") {
val = val[viewInsertSymbol](options);
}

var isFunction = typeof val === "function";

// translate val into a document fragment if it's DOM-like
var frag = makeFrag(isFunction ? "" : val);

// Add a placeholder textNode if necessary.
live.addTextNodeIfNoChildren(frag);
Expand Down
21 changes: 21 additions & 0 deletions test/html-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var NodeLists = require("can-view-nodelist");
var testHelpers = require('can-test-helpers');
var domMutate = require('can-dom-mutate');
var canReflectDeps = require('can-reflect-dependencies');
var canSymbol = require('can-symbol');
var fragment = require("can-fragment");
var queues = require("can-queues");

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

test("html live binding handles objects with can.viewInsert symbol", 2, function(assert) {
var div = document.createElement("div");
var options = {};
var placeholder = document.createTextNode("Placeholder text");
div.appendChild(placeholder);

var html = new Observation(function() {
return {
[canSymbol.for("can.viewInsert")]: function() {
assert.equal(arguments[0], options, "options were passed to symbol function");
return document.createTextNode("Replaced text");
}
};
});

live.html(placeholder, html, div, options);

assert.equal(div.textContent, "Replaced text", "symbol function called");
});

testHelpers.dev.devOnlyTest("child elements must disconnect before parents can re-evaluate", 1,function(){
var observable = new SimpleObservable("value");

Expand Down

0 comments on commit 938167f

Please sign in to comment.