Skip to content

Commit

Permalink
fix: setting properties on loop variable from foreach doesn't work
Browse files Browse the repository at this point in the history
close #129
  • Loading branch information
shepherdwind committed Sep 2, 2020
1 parent ba00784 commit 123f8bc
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version = `cat package.json | grep version | awk -F'"' '{print $$4}'`
SRC = $(wildcard src/*.js src/**/*.js)
TESTS = $(wildcard tests/*.js)
TESTS = $(wildcard test/*.js)
BIN := ./node_modules/.bin
REPORTER ?= spec

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"should": "^7.0.1"
},
"scripts": {
"test": "mocha tests --require should",
"test": "mocha test --require should",
"pub": "npm version patch && npm publish && git push origin master && git push origin --tag"
},
"spm": {
Expand Down
3 changes: 2 additions & 1 deletion src/compile/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module.exports = function(Velocity, utils) {
// @see #25
if (this.condition && this.condition.indexOf('macro:') === 0) {
context = this.context;
} else {
// fix #129
} else if (!context.hasOwnProperty(ref.id)) {
// set var to global context, see #100
context = this.context;
}
Expand Down
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions tests/foreach.test.js → test/foreach.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,27 @@ describe('Loops', function() {
const ret = render(vm, context);
ret.replace(/\s+/g, '').should.equal('matched:"[2]"');
});

it('set ok, fix #129', () => {
const context = {
records: [{ ID: '1' }, { ID: '2' }, { ID: '3' }],
};

const template1 = `
#foreach($item in $records)
#set( $item.key = $item.ID )
#end
$records`;
const template2 = `
#foreach($x in $records)
#set( $item = $x )
#set( $item.key = $item.ID )
#end
$records`;

const ret = render(template1, context);
ret.trim().should.equal('[{ID=1, key=1}, {ID=2, key=2}, {ID=3, key=3}]');
const ret2 = render(template2, context);
ret2.trim().should.equal('[{ID=1, key=1}, {ID=2, key=2}, {ID=3, key=3}]');
});
})
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 123f8bc

Please sign in to comment.