Skip to content

Commit

Permalink
feat(NameExpression): enable binding behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
jdanyow committed Mar 14, 2016
1 parent 09657b7 commit f698c27
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/name-expression.js
Expand Up @@ -9,14 +9,15 @@ function getAU(element) {
}

export class NameExpression {
constructor(sourceExpression, apiName) {
constructor(sourceExpression, apiName, lookupFunctions) {
this.sourceExpression = sourceExpression;
this.apiName = apiName;
this.lookupFunctions = lookupFunctions;
this.discrete = true;
}

createBinding(target) {
return new NameBinder(this.sourceExpression, NameExpression.locateAPI(target, this.apiName));
return new NameBinder(this.sourceExpression, NameExpression.locateAPI(target, this.apiName), this.lookupFunctions);
}

static locateAPI(element: Element, apiName: string): Object {
Expand All @@ -42,9 +43,10 @@ export class NameExpression {
}

class NameBinder {
constructor(sourceExpression, target) {
constructor(sourceExpression, target, lookupFunctions) {
this.sourceExpression = sourceExpression;
this.target = target;
this.lookupFunctions = lookupFunctions;
}

bind(source) {
Expand All @@ -56,15 +58,21 @@ class NameBinder {
}
this.isBound = true;
this.source = source;
this.sourceExpression.assign(this.source, this.target);
if (this.sourceExpression.bind) {
this.sourceExpression.bind(this, source, this.lookupFunctions);
}
this.sourceExpression.assign(this.source, this.target, this.lookupFunctions);
}

unbind() {
if (!this.isBound) {
return;
}
this.isBound = false;
this.sourceExpression.assign(this.source, null);
this.sourceExpression.assign(this.source, null, this.lookupFunctions);
if (this.sourceExpression.unbind) {
this.sourceExpression.unbind(this, this.source);
}
this.source = null;
}
}
24 changes: 23 additions & 1 deletion test/name-expression.spec.js
Expand Up @@ -3,7 +3,8 @@ import {
AccessMember,
AccessKeyed,
LiteralPrimitive,
LiteralString
LiteralString,
BindingBehavior
} from '../src/ast';
import {createScopeForTest} from '../src/scope';
import {NameExpression} from '../src/name-expression';
Expand Down Expand Up @@ -65,4 +66,25 @@ describe('NameExpression', () => {
binding.unbind();
expect(scope.bindingContext.foo[5]).toBe(null);
});

it('supports binding behaviors', () => {
let sourceExpression = new BindingBehavior(new AccessScope('foo'), 'test', []);
let testBehavior = {
bind: jasmine.createSpy('bind'),
unbind: jasmine.createSpy('unbind')
};
let lookupFunctions = {
bindingBehaviors: name => testBehavior,
valueConverters: name => null
};
let expression = new NameExpression(sourceExpression, 'element', lookupFunctions);
let scope = createScopeForTest({});
let binding = expression.createBinding(element);
binding.bind(scope);
expect(scope.bindingContext.foo).toBe(element);
expect(binding.lookupFunctions.bindingBehaviors().bind).toHaveBeenCalledWith(binding, scope);
binding.unbind();
expect(scope.bindingContext.foo).toBe(null);
expect(binding.lookupFunctions.bindingBehaviors().unbind).toHaveBeenCalledWith(binding, scope);
});
});

0 comments on commit f698c27

Please sign in to comment.