Skip to content

Commit

Permalink
Merge pull request #558 from canjs/add-bindings-symbol
Browse files Browse the repository at this point in the history
Make addBindings prefer a symbol.
  • Loading branch information
matthewp authored Jun 26, 2018
2 parents d119a2f + d8c6b96 commit 95158cc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"stop": true,
"global": true,
"Promise": true,
"Map": true
"Map": true,
"WeakMap": true
},
"strict": false,
"curly": true,
Expand Down
16 changes: 15 additions & 1 deletion src/bindings.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
var canReflect = require("can-reflect");
var canSymbol = require('can-symbol');
var viewCallbacks = require("can-view-callbacks");

var bindingsSymbol = canSymbol.for('can.stacheBindings');
var bindingsAdded = new WeakMap();

module.exports = function(bindingsMap) {
canReflect.eachKey(bindingsMap, function(callback, exp){
var map = canReflect.getKeyValue(bindingsMap, bindingsSymbol) || bindingsMap;

// Only add bindings once.
if(bindingsAdded.has(map)) {
return;
} else {
// Would prefer to use WeakSet but IE11 doesn't support it.
bindingsAdded.set(map, true);
}

canReflect.eachKey(map, function(callback, exp){
viewCallbacks.attr(exp, callback);
});
};
27 changes: 26 additions & 1 deletion test/stache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7099,7 +7099,7 @@ function makeTest(name, doc, mutation) {
QUnit.equal(spanTwo.firstChild.nodeValue, "two");
});

test("AddBindings takes a map of bindings", function(){
test("addBindings takes a map of bindings", function(){
var map = new Map();
map.set("foo", function(el, attrData) {
el.appendChild(DOCUMENT().createTextNode("foo"));
Expand All @@ -7119,6 +7119,31 @@ function makeTest(name, doc, mutation) {
QUnit.equal(secondSpan.firstChild.nodeValue, "bar");
});

test("addBindings will use can.stacheBindings symbol if available.", function(){
var map = new Map();
map.set("foo2", function(el, attrData) {
el.appendChild(DOCUMENT().createTextNode("foo"));
});
map.set(/bar2/, function(el, attrData) {
el.appendChild(DOCUMENT().createTextNode("bar"));
});

var bindings = {
bindings: map
};
bindings[canSymbol.for("can.stacheBindings")] = map;
stache.addBindings(bindings);

var template = stache("<span foo2></span><span bar2></span>");
var frag = template();

var firstSpan = frag.firstChild;
var secondSpan = firstSpan.nextSibling;

QUnit.equal(firstSpan.firstChild.nodeValue, "foo");
QUnit.equal(secondSpan.firstChild.nodeValue, "bar");
});

// PUT NEW TESTS RIGHT BEFORE THIS!

}

0 comments on commit 95158cc

Please sign in to comment.