From ff22bd4a5483531a76906481910a8bf2ddfda2a7 Mon Sep 17 00:00:00 2001 From: Justin Meyer Date: Sun, 3 Apr 2022 19:29:11 -0500 Subject: [PATCH] fixes #14 --- can-core.js | 41 ++++++++++++++++++++++++++++++++++++++ view/stache/stache_test.js | 13 ++++++++++++ 2 files changed, 54 insertions(+) diff --git a/can-core.js b/can-core.js index 8099760..d0bf449 100644 --- a/can-core.js +++ b/can-core.js @@ -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 ) { @@ -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; diff --git a/view/stache/stache_test.js b/view/stache/stache_test.js index adaf3b4..620ef7d 100644 --- a/view/stache/stache_test.js +++ b/view/stache/stache_test.js @@ -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('

{{#each this}}'+ + '{{%key}}'+ + '{{/each}}

'); + 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! }