Skip to content

Commit

Permalink
fix(repeat): obey one-time binding behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jdanyow committed Nov 18, 2015
1 parent ff34d22 commit 5fa32e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/repeat.js
Expand Up @@ -69,6 +69,8 @@ export class Repeat {
this.value = 'value';
this.collectionStrategyLocator = collectionStrategyLocator;
this.ignoreMutation = false;
this.sourceExpression = getSourceExpression(this.instruction, 'repeat.for');
this.isOneTime = isOneTime(this.sourceExpression);
}

call(context, changes) {
Expand All @@ -82,7 +84,6 @@ export class Repeat {
*/
bind(bindingContext, overrideContext) {
let items = this.items;
this.sourceExpression = getSourceExpression(this.instruction, 'repeat.for');
this.scope = { bindingContext, overrideContext };
if (items === undefined || items === null) {
return;
Expand All @@ -94,7 +95,6 @@ export class Repeat {
* Unbinds the repeat
*/
unbind() {
this.sourceExpression = null;
this.scope = null;
if (this.collectionStrategy) {
this.collectionStrategy.dispose();
Expand Down Expand Up @@ -184,7 +184,7 @@ export class Repeat {
}

processItemsByStrategy() {
if (!this._observeInnerCollection()) {
if (!this.isOneTime && !this._observeInnerCollection()) {
this._observeCollection();
}
this.collectionStrategy.processItems(this.items);
Expand Down Expand Up @@ -238,3 +238,13 @@ function unwrapExpression(expression) {
}
return unwrapped ? expression : null;
}

function isOneTime(expression) {
while (expression instanceof BindingBehavior) {
if (expression.name === 'oneTime') {
return true;
}
expression = expression.expression;
}
return false;
}
14 changes: 10 additions & 4 deletions test/repeat-integration.spec.js
Expand Up @@ -345,6 +345,16 @@ describe('Repeat array', () => {
nq(() => done());
});
});

it('oneTime does not observe changes', () => {
let template = `<template><div repeat.for="item of items & oneTime">\${item}</div></template>`;
viewModel = { items: [0, 1, 2] };
controller = createController(template, viewModel);
validateState();
expect(hasSubscribers(viewModel, 'items')).toBe(false);
expect(hasArraySubscribers(viewModel.items)).toBe(false);
controller.unbind();
});
});

describe('Repeat map', () => {
Expand All @@ -358,7 +368,3 @@ describe('Repeat number', () => {
describe('Repeat object converted to collection', () => {
let viewModel, controller;
});

describe('Repeat oneTime', () => {
let viewModel, controller;
});

0 comments on commit 5fa32e9

Please sign in to comment.