Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5afc219

Browse files
make $index work with #merge as well
1 parent 3a2f1e1 commit 5afc219

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

‎st.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,19 @@
312312
result[key] = res[key];
313313
}
314314
});
315+
// clean up $index from the result
316+
// necessary because #merge merges multiple objects into one,
317+
// and one of them may be 'this', in which case the $index attribute
318+
// will have snuck into the final result
319+
if(typeof data === 'object') {
320+
delete result["$index"];
321+
} else {
322+
delete String.prototype.$index;
323+
delete Number.prototype.$index;
324+
delete Function.prototype.$index;
325+
delete Array.prototype.$index;
326+
delete Boolean.prototype.$index;
327+
}
315328
}
316329
} else if (fun.name === '#each') {
317330
// newData will be filled with parsed results

‎test/unit/transform/loop.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,44 @@ describe('#each', function(){
116116
];
117117
compare(actual, expected);
118118
})
119+
it('use with #merge', function() {
120+
var template = {
121+
"users": {
122+
"{{#each $root.$get.users}}": {
123+
"{{#merge}}": [
124+
"{{this}}",
125+
{ "balance": "{{$root.$jason[$index]}}" , "index": "{{$index}}" }
126+
]
127+
}
128+
}
129+
};
130+
var data = {
131+
"$get": {
132+
"users": [{
133+
"id": "0xdef",
134+
"username": "Alice"
135+
}, {
136+
"id": "0xabc",
137+
"username": "Bob"
138+
}]
139+
},
140+
"$jason": [100, 58]
141+
}
142+
var actual = st.TRANSFORM.transform(template, data);
143+
compare(actual, {
144+
"users": [{
145+
"id": "0xdef",
146+
"username": "Alice",
147+
"balance": 100,
148+
"index": 0
149+
}, {
150+
"id": "0xabc",
151+
"username": "Bob",
152+
"balance": 58,
153+
"index": 1
154+
}]
155+
})
156+
});
119157
});
120158
/*
121159
it('#each with $index', function() {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /