From 0c604d590406302f72c477e9a489334f2b4921a3 Mon Sep 17 00:00:00 2001 From: David Mitchell Date: Sun, 12 May 2013 12:57:50 +1200 Subject: [PATCH] Added tests (42,43) for mapping objects/arrays when using different values in the map for the data keys and the attribute values. ex map.class('name').use('fullname') Additional the proposed solution for the problem. Also removed a passing of a key to the iterate method when the data is of type object as it is not used. --- lib/plates.js | 4 ++-- test/api-test.js | 25 +++++++++++++++++++++++++ test/fixtures/test-42.html | 5 +++++ test/fixtures/test-42.json | 5 +++++ test/fixtures/test-42.out | 5 +++++ test/fixtures/test-43.html | 6 ++++++ test/fixtures/test-43.json | 6 ++++++ test/fixtures/test-43.out | 6 ++++++ 8 files changed, 60 insertions(+), 2 deletions(-) create mode 100755 test/fixtures/test-42.html create mode 100755 test/fixtures/test-42.json create mode 100755 test/fixtures/test-42.out create mode 100755 test/fixtures/test-43.html create mode 100755 test/fixtures/test-43.json create mode 100755 test/fixtures/test-43.out diff --git a/lib/plates.js b/lib/plates.js index 85b9723..7d4d0bc 100644 --- a/lib/plates.js +++ b/lib/plates.js @@ -360,12 +360,12 @@ function matchClosing(input, tagname, html) { newdata = tagbody + newdata; if (Array.isArray(v)) { - newdata = that.iterate(html, v, components, tagname, value, map); + newdata = that.iterate(html, v, components, tagname, mapping.dataKey, map); // If the item is an array, then we need to tell // Plates that we're dealing with nests that.nest.push(tagname); } else if (typeof v === 'object') { - newdata = tagbody + that.iterate(html, v, components, tagname, value, map); + newdata = tagbody + that.iterate(html, v, components, tagname, null, map); } buffer += newdata || ''; diff --git a/test/api-test.js b/test/api-test.js index 5d2630e..9a8cd03 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -504,6 +504,31 @@ vows.describe('merge data into markup').addBatch({ return common.createTest('test-41', map); }() + ), + + '(42) Should bind according to the mapping for an array': ( + + function() { + var map = Plates.Map(); + + map.class("names").use("names"); + map.class("last").use("lastname"); + + return common.createTest('test-42', map); + }() + ), + + '(43) Should bind to according to the mapping for an object': ( + + function() { + var map = Plates.Map(); + + map.class("name").use("fullname"); + map.class("first").use("firstname"); + map.class("last").use("lastname"); + + return common.createTest('test-43', map); + }() ) } diff --git a/test/fixtures/test-42.html b/test/fixtures/test-42.html new file mode 100755 index 0000000..4d1a3dd --- /dev/null +++ b/test/fixtures/test-42.html @@ -0,0 +1,5 @@ +
+
+
+
+
\ No newline at end of file diff --git a/test/fixtures/test-42.json b/test/fixtures/test-42.json new file mode 100755 index 0000000..ba7f209 --- /dev/null +++ b/test/fixtures/test-42.json @@ -0,0 +1,5 @@ +{ + "names" : { + "lastname": [1,2,3] + } +} \ No newline at end of file diff --git a/test/fixtures/test-42.out b/test/fixtures/test-42.out new file mode 100755 index 0000000..f4f052e --- /dev/null +++ b/test/fixtures/test-42.out @@ -0,0 +1,5 @@ +
+
+
1
2
3
+
+
\ No newline at end of file diff --git a/test/fixtures/test-43.html b/test/fixtures/test-43.html new file mode 100755 index 0000000..edaf5d8 --- /dev/null +++ b/test/fixtures/test-43.html @@ -0,0 +1,6 @@ +
+
+
+
+
+
\ No newline at end of file diff --git a/test/fixtures/test-43.json b/test/fixtures/test-43.json new file mode 100755 index 0000000..c482407 --- /dev/null +++ b/test/fixtures/test-43.json @@ -0,0 +1,6 @@ +{ + "fullname" : { + "firstname": "bob", + "lastname": "jones" + } + } diff --git a/test/fixtures/test-43.out b/test/fixtures/test-43.out new file mode 100755 index 0000000..70a57f4 --- /dev/null +++ b/test/fixtures/test-43.out @@ -0,0 +1,6 @@ +
+
+
bob
+
jones
+
+
\ No newline at end of file