Permalink
Browse files

feat(Binding): fromView, toView

  • Loading branch information...
bigopon authored and jdanyow committed Sep 15, 2017
1 parent a18fd33 commit f232a734235038da8d6d8026028667a517105f88
Showing with 43 additions and 5 deletions.
  1. +7 −3 src/binding-expression.js
  2. +3 −1 src/binding-mode.js
  3. +33 −1 test/binding-expression.spec.js
View
@@ -89,21 +89,25 @@ export class Binding {
let mode = this.mode;
if (!this.targetObserver) {
let method = mode === bindingMode.twoWay ? 'getObserver' : 'getAccessor';
let method = mode === bindingMode.twoWay || mode === bindingMode.fromView ? 'getObserver' : 'getAccessor';
this.targetObserver = this.observerLocator[method](this.target, this.targetProperty);
}
if ('bind' in this.targetObserver) {
this.targetObserver.bind();
}
let value = this.sourceExpression.evaluate(source, this.lookupFunctions);
this.updateTarget(value);
if (this.mode !== bindingMode.fromView) {
let value = this.sourceExpression.evaluate(source, this.lookupFunctions);
this.updateTarget(value);
}
if (mode === bindingMode.oneWay) {
enqueueBindingConnect(this);
} else if (mode === bindingMode.twoWay) {
this.sourceExpression.connect(this, source);
this.targetObserver.subscribe(targetContext, this);
} else if (mode === bindingMode.fromView) {
this.targetObserver.subscribe(targetContext, this);
}
}
View
@@ -1,5 +1,7 @@
export const bindingMode = {
oneTime: 0,
oneWay: 1,
twoWay: 2
twoWay: 2,
toView: 1,
fromView: 3
};
@@ -4,6 +4,7 @@ import {bindingMode} from '../src/binding-mode';
import {BindingEngine} from '../src/binding-engine';
import {checkDelay} from './shared';
import {createScopeForTest} from '../src/scope';
import {sourceContext} from '../src/connectable-binding';
describe('BindingExpression', () => {
let bindingEngine;
@@ -13,7 +14,7 @@ describe('BindingExpression', () => {
bindingEngine.observerLocator.dirtyChecker.checkDelay = checkDelay;
});
it('handles AccessMember', done => {
it('handles AccessMember in twoWay mode', done => {
let source = { foo: { bar: 'baz'} };
let target = document.createElement('input');
let bindingExpression = bindingEngine.createBindingExpression('value', 'foo.bar', bindingMode.twoWay);
@@ -35,6 +36,37 @@ describe('BindingExpression', () => {
}, checkDelay * 2);
});
it('handles AccessMember in fromView mode', done => {
let source = { foo: { bar: 'baz'} };
let target = document.createElement('input');
let bindingExpression = bindingEngine.createBindingExpression('value', 'foo.bar', bindingMode.fromView);
let binding = bindingExpression.createBinding(target);
binding.bind(createScopeForTest(source));
expect(target.value).toBe('');
let sourceObserver = bindingEngine.observerLocator.getObserver(source.foo, 'bar');
expect(sourceObserver.hasSubscribers()).toBe(false);
expect(binding.targetObserver.hasSubscribers()).toBe(true);
expect(binding.targetObserver.hasSubscriber(sourceContext, sourceObserver)).toBe(false);
source.foo.bar = 'xup';
setTimeout(() => {
expect(target.value).toBe('');
target.value = 'xup';
target.dispatchEvent(new CustomEvent('input'));
setTimeout(() => {
expect(source.foo.bar).toBe('xup');
binding.unbind();
expect(binding.targetObserver.hasSubscribers()).toBe(false);
done();
}, checkDelay * 2);
}, checkDelay * 2);
});
it('handles ValueConverter', done => {
let valueConverters = {
numberToString: { toView: value => value.toString(), fromView: value => parseInt(value) },

0 comments on commit f232a73

Please sign in to comment.