Skip to content

Commit

Permalink
Fix bug on loops with condition combination.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Sep 16, 2016
1 parent 425a46e commit 6c232c6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
31 changes: 27 additions & 4 deletions spec/regression.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ describe('Regression', function () {

view.update(data1);
expect(view).toBe('<ul><li class="selected"><span>USD</span>: US dollar</li><!--if--><li><span>EUR</span>: Euro</li><!--if--><li><span>AUD</span>: Australian dollar</li><!--if--></ul>');


var data2 = {
"currency": "AUD",
"locale": "en",
"currencies": {"USD": {"name": "US dollar"}, "EUR": {"name": "Euro"}, "AUD": {"name": "Australian dollar"}}
};

view.update(data2);
expect(view).toBe('<ul><li><span>USD</span>: US dollar</li><!--if--><li><span>EUR</span>: Euro</li><!--if--><li class="selected"><span>AUD</span>: Australian dollar</li><!--if--></ul>');

});

it('loos should update two levels loops once', function () {
Expand Down Expand Up @@ -190,4 +190,27 @@ describe('Regression', function () {
expect(view).toBe('<ol><li>red</li><li>green</li><li>blue</li><!--for--><li>red</li><li>green</li><li>blue</li><!--for--></ol>');
});

it('loops with cond and outer scope', function () {
var view = Monkberry.render(ReLoopWithIfAndOuterScope, root);
view.update({
outer: "outer",
attributes: [
{
name: "name1",
value: "value1"
},
{
name: "name2",
value: "value2"
}
]
});

expect(view).toBe('<div><span>name1</span><span>value1</span><span>outer</span><!--if--><span>name2</span><span>value2</span><span>outer</span><!--if--></div>');
view.update({
outer: "outer2"
});
expect(view).toBe('<div><span>name1</span><span>value1</span><span>outer2</span><!--if--><span>name2</span><span>value2</span><span>outer2</span><!--if--></div>');
});

});
11 changes: 11 additions & 0 deletions spec/views/regression.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,14 @@
{% endfor %}
</ol>
</ReLoopMustWorkWithSameNameLoops>
<ReLoopWithIfAndOuterScope>
<div>
{% for attr of attributes %}
<span>{{attr.name}}</span>
<span>{{attr.value}}</span>
{% if attr %}
<span>{{outer}}</span>
{% endif %}
{% endfor %}
</div>
</ReLoopWithIfAndOuterScope>
13 changes: 12 additions & 1 deletion src/compiler/for.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,20 @@ export default {
}

figure.addOnUpdate(
node.options === null ?
sourceNode(node.loc, [
` ${childrenName}.forEach(function (view) {\n`,
node.options === null ? `` : ` view.update(__data__);\n`,
` view.update(view.__state__);\n`,
` });`
]) :
// TODO: Remove double update on foreach.
// Simple solution is to use Object.assign({}, __data__, view.__state__),
// But this isn't supported in a lot of browsers for now.
// Also i have solution for this what may come in next v5 version...
sourceNode(node.loc, [
` ${childrenName}.forEach(function (view) {\n`,
` view.update(view.__state__);\n`,
` view.update(__data__);\n`,
` view.update(view.__state__);\n`,
` });`
])
Expand Down

0 comments on commit 6c232c6

Please sign in to comment.