Skip to content

Commit

Permalink
fixes #905
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Apr 28, 2014
1 parent 1c2aa77 commit 9a15db9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
12 changes: 9 additions & 3 deletions view/bindings/bindings.js
Expand Up @@ -50,7 +50,13 @@ steal("can/util", "can/view/mustache", "can/control", function (can) {
return !!editable(el.parentNode);
}
};
})();
})(),
removeCurly = function(value){
if(value[0] === "{" && value[value.length-1] === "}") {
return value.substr(1, value.length - 2);
}
return value;
};

// ## can-value
// Implement the `can-value` special attribute
Expand All @@ -63,7 +69,7 @@ steal("can/util", "can/view/mustache", "can/control", function (can) {
// should be a string representing some value in the current scope to cross-bind to.
can.view.attr("can-value", function (el, data) {

var attr = el.getAttribute("can-value"),
var attr = removeCurly(el.getAttribute("can-value")),
// Turn the attribute passed in into a compute. If the user passed in can-value="name" and the current
// scope of the template is some object called data, the compute representing this can-value will be the
// data.attr('name') property.
Expand Down Expand Up @@ -175,7 +181,7 @@ steal("can/util", "can/view/mustache", "can/control", function (can) {
handler = function (ev) {
// The attribute value, representing the name of the method to call (i.e. can-submit="foo" foo is the
// name of the method)
var attr = el.getAttribute(attributeName),
var attr = removeCurly( el.getAttribute(attributeName) ),
scopeData = data.scope.read(attr, {
returnObserveMethods: true,
isArgument: true
Expand Down
68 changes: 68 additions & 0 deletions view/bindings/bindings_test.js
Expand Up @@ -439,5 +439,73 @@ steal("can/view/bindings", "can/map", "can/test", function (special) {
equal(map.attr("age"), "32", "updated from contenteditable");
});

test("can-event handlers work with {} (#905)", function () {
expect(4);
var template = can.mustache("<div>" +
"{{#each foodTypes}}" +
"<p can-click='{doSomething}'>{{content}}</p>" +
"{{/each}}" +
"</div>");

var foodTypes = new can.List([{
title: "Fruits",
content: "oranges, apples"
}, {
title: "Breads",
content: "pasta, cereal"
}, {
title: "Sweets",
content: "ice cream, candy"
}]);
var doSomething = function (foodType, el, ev) {
ok(true, "doSomething called");
equal(el[0].nodeName.toLowerCase(), "p", "this is the element");
equal(ev.type, "click", "1st argument is the event");
equal(foodType, foodTypes[0], "2nd argument is the 1st foodType");

};

var frag = template({
foodTypes: foodTypes,
doSomething: doSomething
});

var ta = document.getElementById("qunit-test-area");
ta.appendChild(frag);
var p0 = ta.getElementsByTagName("p")[0];
can.trigger(p0, "click");

});

test("can-value works with {} (#905)", function () {

var template = can.mustache("<input can-value='{age}'/>");

var map = new can.Map();

var frag = template(map);

var ta = document.getElementById("qunit-test-area");
ta.appendChild(frag);

var input = ta.getElementsByTagName("input")[0];
equal(input.value, "", "input value set correctly if key does not exist in map");

map.attr("age", "30");

equal(input.value, "30", "input value set correctly");

map.attr("age", "31");

equal(input.value, "31", "input value update correctly");

input.value = "32";

can.trigger(input, "change");

equal(map.attr("age"), "32", "updated from input");

});


});

0 comments on commit 9a15db9

Please sign in to comment.