Skip to content

Commit

Permalink
fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Apr 4, 2022
1 parent aaafbe1 commit ff22bd4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
41 changes: 41 additions & 0 deletions can-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ compute.set = stacheKey.set;
List.prototype.each = List.prototype.forEach;

var specialRead = {index: true, key: true, event: true, element: true, viewModel: true};

var baseObjectRead = stacheKey.propertyReadersMap.object.read;
stacheKey.propertyReadersMap.object.read = function compatabilityObjectRead(value, prop, reads, options, state, prev){
if(options.can23Compatibility ) {
Expand All @@ -58,6 +59,46 @@ stacheKey.propertyReadersMap.object.read = function compatabilityObjectRead(valu
return baseObjectRead.apply(this, arguments);
}
};
/*
var baseMapRead = stacheKey.propertyReadersMap.map.read;
stacheKey.propertyReadersMap.map.read = function compatabilityMapRead(value, prop, reads, options, state, prev){
if(options.can23Compatibility ) {
var valueType = typeof value;
if(value == null || (valueType !== 'object' && valueType !== 'function')) {
return undefined;
} else {
if(prop.key in value) {
return value[prop.key];
}
// TODO: remove in 3.0. This is for backwards compat with @key and @index.
else if( prop.at && specialRead[prop.key] && ( ("@"+prop.key) in value)) {
prop.at = false;
return value["@"+prop.key];
}
}
} else {
debugger;
return baseMapRead.apply(this, arguments);
}
};
*/

// The following tries to fix reading {{foo.bar}}
// from a map like new CanMap({"foo.bar" :"zed"});
// This more or less lets CanMap figure out how to read
var oldStacheKeyGet = stacheKey.get;
stacheKey.get = function(parent, readsOrString, options){
var reads = stacheKey.reads(readsOrString);
if( reads.length > 1 &&
parent instanceof Map &&
Array.prototype.filter.call( reads, function(read){ return read.at }).length === 0 ) {
var singleRead = {key: Array.prototype.map.call( reads, function(read){ return read.key }).join(".") };
return stacheKey.read(parent, [singleRead], options || {}).value;
}
return stacheKey.read(parent, reads, options || {}).value;
}


var CanJSNames = {Control: 1, LetContext: 1, DefineList: 1};
var constrctorCreated = Construct._created;
Expand Down
13 changes: 13 additions & 0 deletions view/stache/stache_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4995,6 +4995,19 @@ function skip(test) {
equal((span[1].innerHTML), '1', 'iteration for %index');
});

test("Each with keys that have . like 'foo.bar' works (#14)", function () {
var template = can.stache('<p>{{#each this}}'+
'<span>{{%key}}</span><label>{{this}}</label>'+
'{{/each}}</p>');
var data = new can.Map({ "foo.bar": "baz" });
var dom = template(data);
var div = doc.createElement('div');
div.appendChild(dom);
console.log(div.innerHTML);
equal(div.getElementsByTagName('span')[0].innerHTML, "foo.bar", "key");
equal(div.getElementsByTagName('label')[0].innerHTML, "baz", "value");
});

// PUT NEW TESTS RIGHT BEFORE THIS!
}

Expand Down

0 comments on commit ff22bd4

Please sign in to comment.