Skip to content

Commit

Permalink
Fix bug with loop options cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jul 6, 2016
1 parent 2084a14 commit e1440c7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
24 changes: 24 additions & 0 deletions spec/regression.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,28 @@ describe('Regression', function () {
expect(view).toBe('<p><i>1</i><!--for--><i>2</i><!--for--><i>3</i><!--for--><!--for--></p>');
});

it('should cache options variable data for loops', function () {
var view = Monkberry.render(ReLoopOptionsCache, root);

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

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>');

});

});
15 changes: 15 additions & 0 deletions spec/views/regression.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,18 @@
{% endif %}
</p>
</UpdateLocalVars>
<ReLoopOptionsCache>
<ul>
{% for code, item of currencies %}
{% if code == currency %}
<li class="selected">
<span>{{ code }}</span>{{ ': ' + item.name }}
</li>
{% else %}
<li>
<span>{{ code }}</span>{{ ': ' + item.name }}
</li>
{% endif %}
{% endfor %}
</ul>
</ReLoopOptionsCache>
15 changes: 12 additions & 3 deletions src/compiler/for.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ export default {
])
);

// Add to child(!) figure extra cache methods,
// for saving data from loop options for render.
[node.options.value, node.options.key].forEach(variable => {
if (subfigure.hasSpot(variable)) {
subfigure.spot(variable).onlyFromLoop = true;
}
subfigure.thisRef = true;
subfigure.prependOnUpdate(sourceNode([
` if (_this.__cache__.${variable}) {\n`,
` __data__.${variable} = _this.__cache__.${variable};\n`,
` }`
]));

// Cache all options data.
subfigure.spot(variable).onlyFromLoop = true;
subfigure.spot(variable).cache = true;
});

}
Expand Down
27 changes: 23 additions & 4 deletions src/figure.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export class Figure {
this.onUpdate = [];
this.onRemove = [];
this.thisRef = false;
this.spotMaxLength = 0;
}

generate() {
Expand All @@ -45,7 +44,7 @@ export class Figure {
` Monkberry.call(this);\n`
]);

if (this.spotMaxLength > 1) {
if (this.isCacheNeeded()) {
sn.add(` this.__cache__ = {};\n`);
}

Expand Down Expand Up @@ -234,14 +233,30 @@ export class Figure {
for (let variable of s.variables) {
this.spot(variable).cache = true;
}

this.spotMaxLength = s.variables.length;
}
}

return this.spots[s.reference];
}

isCacheNeeded() {
let needed = false;

Object.keys(this.spots)
.map(x => this.spots[x])
.forEach(spot => {
if (spot.variables.length > 1) {
needed = true;
}
if (spot.cache) {
needed = true;
}
});

return needed;
}


root() {
if (this.parent) {
return this.parent.root();
Expand Down Expand Up @@ -296,6 +311,10 @@ export class Figure {
this.onUpdate.push(node);
}

prependOnUpdate(node) {
this.onUpdate.unshift(node);
}

addOnRemove(node) {
this.onRemove.push(node);
}
Expand Down

0 comments on commit e1440c7

Please sign in to comment.