Skip to content

Commit

Permalink
.with(some_func) extension (transform/map values when binding - see
Browse files Browse the repository at this point in the history
test-10)
  • Loading branch information
kkolman committed Feb 13, 2012
1 parent e8e9ad4 commit bae1fae
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/browser/plates.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
tagbody = tagbody.replace(this.attr, function(str, key, value, a) {

var newdata = map[ii].dataKey ? data[map[ii].dataKey] : data[key];
if (mappings[ii].transform) {
newdata = mappings[ii].transform(newdata);
}

if (map[ii].replace === key) {

Expand Down Expand Up @@ -200,6 +203,10 @@
as: function(val) {
last.call(this).replace = val;
return this;
},
with: function(val) {
last.call(this).transform = val;
return this;
}
};

Expand Down
8 changes: 8 additions & 0 deletions lib/plates.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@
tagbody = tagbody.replace(this.attr, function(str, key, value, a) {

var newdata = mappings[ii].dataKey ? data[mappings[ii].dataKey] : data[key];

if (mappings[ii].transform) {
newdata = mappings[ii].transform(newdata);
}

if (mappings[ii].replace === key) {

Expand Down Expand Up @@ -219,6 +223,10 @@
as: function(val) {
last.call(this).replace = val;
return this;
},
with: function(val) {
last.call(this).transform = val;
return this;
}
};

Expand Down
15 changes: 15 additions & 0 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,23 @@ vows.describe('merge data into markup').addBatch({
return common.createTest('test-9', map);

}()

),

'(10) using with val transformation': (
function() {

var map = Plates.Map();

map.class('snippet').to('foo').with(function(val) { return val.toUpperCase()});
map.class('thumb').use('bar').as('src').with(function(val) { return "http://prefix/" + val});

return common.createTest('test-10', map);

}()

)

}

}).export(module);
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/test-10.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div class="snippet"></div>
<img class="thumb" src="" />
4 changes: 4 additions & 0 deletions test/fixtures/test-10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "fooval",
"bar": "barval"
}
2 changes: 2 additions & 0 deletions test/fixtures/test-10.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div class="snippet">FOOVAL</div>
<img class="thumb" src="http://prefix/barval" />

0 comments on commit bae1fae

Please sign in to comment.