Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change stacheKey.get to check for deep properties and change to reading a single key #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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