Browse files

[fix] nested objects now work with maps

  • Loading branch information...
1 parent 40cb8b7 commit 4620d18af0b086c73896765122c03baf1ee5f9e1 @pgte pgte committed Aug 23, 2012
View
14 lib/plates.js
@@ -58,7 +58,7 @@ var Plates = (typeof module !== 'undefined' && typeof module.exports !== 'undefi
return str.split(' ').indexOf(className) > -1;
},
- iterate: function(html, value, components, tagname, key) {
+ iterate: function(html, value, components, tagname, key, map) {
var output = '',
segment = html.slice(
@@ -96,7 +96,7 @@ var Plates = (typeof module !== 'undefined' && typeof module.exports !== 'undefi
// We need to refine the selection now that we know we're dealing with a
// nested object
segment = segment.slice(components.input.length, -(tagname.length + 3));
- return output += this.bind(segment, value);
+ return output += this.bind(segment, value, map);
}
@@ -122,7 +122,7 @@ var Plates = (typeof module !== 'undefined' && typeof module.exports !== 'undefi
var openers = 0,
components,
attributes,
- mappings = map && map.mappings,
+ mappings = map && compileMappings(map.mappings),
intag = false,
tagname = '',
isClosing = false,
@@ -193,8 +193,6 @@ var Plates = (typeof module !== 'undefined' && typeof module.exports !== 'undefi
//
if (mappings && mappings.length > 0) {
- mappings = compileMappings(mappings);
-
for (var ii = mappings.length - 1; ii >= 0; ii--) {
var setAttribute = false
@@ -240,14 +238,14 @@ var Plates = (typeof module !== 'undefined' && typeof module.exports !== 'undefi
if (isArray(v)) {
- newdata = that.iterate(html, v, components, tagname, value);
+ newdata = that.iterate(html, v, components, tagname, value, 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);
+ newdata = tagbody + that.iterate(html, v, components, tagname, value, map);
}
buffer += newdata || '';
@@ -293,7 +291,7 @@ var Plates = (typeof module !== 'undefined' && typeof module.exports !== 'undefi
var v = data[value],
nest = isArray(v),
- output = (nest || typeof v === 'object') ? that.iterate(html, v, components, tagname, value) : v;
+ output = (nest || typeof v === 'object') ? that.iterate(html, v, components, tagname, value, map) : v;
// If the item is an array, then we need to tell
// Plates that we're dealing with nests
View
28 test/api-test.js
@@ -295,6 +295,34 @@ vows.describe('merge data into markup').addBatch({
return common.createTest('test-25', map);
}()
+ ),
+
+ '(26) complex nesting should work as expected': (
+
+ function() {
+ var map = Plates.Map();
+ map.class('author').use('author');
+ map.class('name').use('name');
+ map.class('name').use('link').as('href');
+ map.class('title').use('title');
+ map.class('inner').use('inner');
+
+ return common.createTest('test-26', map);
+ }()
+ ),
+
+ '(27) complex nesting with arrays should work as expected': (
+
+ function() {
+ var map = Plates.Map();
+ map.class('author').use('author');
+ map.class('name').use('name');
+ map.class('name').use('link').as('href');
+ map.class('title').use('title');
+ map.class('inner').use('inner');
+
+ return common.createTest('test-26', map);
+ }()
)
View
8 test/common.js
@@ -11,12 +11,12 @@ function get(name, extension) {
__dirname +
'/fixtures/' +
name + '.' +
- extension
- ).toString();
+ extension, 'utf8'
+ );
} catch(e) {
return null;
}
-};
+}
common.render = function(name, data, map) {
@@ -38,7 +38,7 @@ common.createTest = function(name, map) {
};
},
'should merge data to markup': function(result) {
- assert.equal(result.render, this.out);
+ assert.equal(this.out, result.render);
}
};
View
8 test/fixtures/test-26.html
@@ -0,0 +1,8 @@
+<div class="authors">
+ <ul>
+ <li class="author">
+ <h2 class="title"></h2>
+ <a class="name" href="">The author name is<span class="inner"></span></a>
+ </li>
+ </ul>
+</div>
View
4 test/fixtures/test-26.json
@@ -0,0 +1,4 @@
+{
+ "author":
+ { "name": {"inner": "Author 1"}, "title": "Title 1", "link": "//1" }
+}
View
8 test/fixtures/test-26.out
@@ -0,0 +1,8 @@
+<div class="authors">
+ <ul>
+ <li class="author">
+ <h2 class="title">Title 1</h2>
+ <a class="name" href="//1">The author name is<span class="inner">Author 1</span></a>
+ </li>
+ </ul>
+</div>
View
8 test/fixtures/test-27.html
@@ -0,0 +1,8 @@
+<div class="authors">
+ <ul>
+ <li class="author">
+ <h2 class="title"></h2>
+ <a class="name" href="">The author name is<span class="inner"></span></a>
+ </li>
+ </ul>
+</div>
View
7 test/fixtures/test-27.json
@@ -0,0 +1,7 @@
+{
+ "author": [
+ { "name": {"inner": "Author 1"}, "title": "Title 1", "link": "//1" },
+ { "name": {"inner": "Author 2"}, "title": "Title 2", "link": "//2" },
+ { "name": {"inner": "Author 3"}, "title": "Title 3", "link": "//3" }
+ ]
+}
View
14 test/fixtures/test-27.out
@@ -0,0 +1,14 @@
+<div class="authors">
+ <ul>
+ <li class="author">
+ <h2 class="title">Title 1</h2>
+ <a class="name" href="//1">The author name is<span class="inner">Author 1</span></a>
+ </li><li class="author">
+ <h2 class="title">Title 2</h2>
+ <a class="name" href="//2">The author name is<span class="inner">Author 2</span></a>
+ </li><li class="author">
+ <h2 class="title">Title 3</h2>
+ <a class="name" href="//3">The author name is<span class="inner">Author 3</span></a>
+ </li>
+ </ul>
+</div>

0 comments on commit 4620d18

Please sign in to comment.