Remove 2-way binding from objects
This commit is contained in:
@@ -109,7 +109,7 @@ describe('ol.Object', function() {
|
||||
|
||||
describe('notify', function() {
|
||||
|
||||
var listener1, listener2, listener3;
|
||||
var listener1, listener2;
|
||||
|
||||
beforeEach(function() {
|
||||
listener1 = sinon.spy();
|
||||
@@ -117,11 +117,6 @@ describe('ol.Object', function() {
|
||||
|
||||
listener2 = sinon.spy();
|
||||
goog.events.listen(o, ol.ObjectEventType.PROPERTYCHANGE, listener2);
|
||||
|
||||
var o2 = new ol.Object();
|
||||
o2.bindTo('k', o);
|
||||
listener3 = sinon.spy();
|
||||
goog.events.listen(o2, 'change:k', listener3);
|
||||
});
|
||||
|
||||
it('dispatches events', function() {
|
||||
@@ -143,21 +138,11 @@ describe('ol.Object', function() {
|
||||
expect(event.key).to.be('k');
|
||||
expect(event.oldValue).to.be(1);
|
||||
});
|
||||
|
||||
it('dispatches events to bound objects', function() {
|
||||
o.notify('k', 1);
|
||||
expect(listener3.calledOnce).to.be(true);
|
||||
var args = listener3.firstCall.args;
|
||||
expect(args).to.have.length(1);
|
||||
var event = args[0];
|
||||
expect(event.key).to.be('k');
|
||||
expect(event.oldValue).to.be(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('set', function() {
|
||||
|
||||
var listener1, o2, listener2, listener3;
|
||||
var listener1, listener2;
|
||||
|
||||
beforeEach(function() {
|
||||
listener1 = sinon.spy();
|
||||
@@ -165,11 +150,6 @@ describe('ol.Object', function() {
|
||||
|
||||
listener2 = sinon.spy();
|
||||
goog.events.listen(o, ol.ObjectEventType.PROPERTYCHANGE, listener2);
|
||||
|
||||
o2 = new ol.Object();
|
||||
o2.bindTo('k', o);
|
||||
listener3 = sinon.spy();
|
||||
goog.events.listen(o2, 'change:k', listener3);
|
||||
});
|
||||
|
||||
it('dispatches events to object', function() {
|
||||
@@ -177,7 +157,6 @@ describe('ol.Object', function() {
|
||||
expect(listener1).to.be.called();
|
||||
|
||||
expect(o.getKeys()).to.eql(['k']);
|
||||
expect(o2.getKeys()).to.eql(['k']);
|
||||
});
|
||||
|
||||
it('dispatches generic change events to object', function() {
|
||||
@@ -189,259 +168,6 @@ describe('ol.Object', function() {
|
||||
expect(event.key).to.be('k');
|
||||
});
|
||||
|
||||
it('dispatches events to bound object', function() {
|
||||
o.set('k', 1);
|
||||
expect(listener3).to.be.called();
|
||||
});
|
||||
|
||||
it('dispatches events to object bound to', function() {
|
||||
o2.set('k', 2);
|
||||
expect(listener1).to.be.called();
|
||||
|
||||
expect(o.getKeys()).to.eql(['k']);
|
||||
expect(o2.getKeys()).to.eql(['k']);
|
||||
});
|
||||
|
||||
it('dispatches generic change events to object bound to', function() {
|
||||
o2.set('k', 2);
|
||||
expect(listener2.calledOnce).to.be(true);
|
||||
var args = listener2.firstCall.args;
|
||||
expect(args).to.have.length(1);
|
||||
var event = args[0];
|
||||
expect(event.key).to.be('k');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('bind', function() {
|
||||
|
||||
var o2;
|
||||
|
||||
beforeEach(function() {
|
||||
o2 = new ol.Object();
|
||||
});
|
||||
|
||||
describe('bindTo after set', function() {
|
||||
|
||||
beforeEach(function() {
|
||||
o.set('k', 1);
|
||||
o2.set('k', 0);
|
||||
});
|
||||
|
||||
it('gets expected value', function() {
|
||||
o2.bindTo('k', o);
|
||||
expect(o.get('k')).to.eql(1);
|
||||
expect(o2.get('k')).to.eql(1);
|
||||
|
||||
expect(o.getKeys()).to.eql(['k']);
|
||||
expect(o2.getKeys()).to.eql(['k']);
|
||||
});
|
||||
|
||||
it('dispatches a change: event', function() {
|
||||
var listener = sinon.spy();
|
||||
o2.on('change:k', listener);
|
||||
o2.bindTo('k', o);
|
||||
expect(listener.calledOnce).to.be(true);
|
||||
var call = listener.firstCall;
|
||||
expect(call.args).to.have.length(1);
|
||||
expect(call.args[0].key).to.be('k');
|
||||
expect(call.args[0].oldValue).to.be(0);
|
||||
expect(o2.get('k')).to.be(1);
|
||||
});
|
||||
|
||||
it('dispatches a propertychange event', function() {
|
||||
var listener = sinon.spy();
|
||||
o2.on('propertychange', listener);
|
||||
o2.bindTo('k', o);
|
||||
expect(listener.calledOnce).to.be(true);
|
||||
var call = listener.firstCall;
|
||||
expect(call.args).to.have.length(1);
|
||||
expect(call.args[0].key).to.be('k');
|
||||
expect(call.args[0].oldValue).to.be(0);
|
||||
expect(o2.get('k')).to.be(1);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('bindTo before set', function() {
|
||||
|
||||
it('gets expected value', function() {
|
||||
o2.bindTo('k', o);
|
||||
o.set('k', 1);
|
||||
expect(o.get('k')).to.eql(1);
|
||||
expect(o2.get('k')).to.eql(1);
|
||||
|
||||
expect(o.getKeys()).to.eql(['k']);
|
||||
expect(o2.getKeys()).to.eql(['k']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('backwards', function() {
|
||||
describe('bindTo after set', function() {
|
||||
|
||||
it('gets expected value', function() {
|
||||
o2.set('k', 1);
|
||||
o2.bindTo('k', o);
|
||||
expect(o.get('k')).to.be(undefined);
|
||||
expect(o2.get('k')).to.be(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bindTo before set', function() {
|
||||
|
||||
it('gets expected value', function() {
|
||||
o2.bindTo('k', o);
|
||||
o2.set('k', 1);
|
||||
expect(o.get('k')).to.eql(1);
|
||||
expect(o2.get('k')).to.eql(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('unbind', function() {
|
||||
var o2;
|
||||
|
||||
beforeEach(function() {
|
||||
o2 = new ol.Object();
|
||||
o2.bindTo('k', o);
|
||||
o2.set('k', 1);
|
||||
});
|
||||
|
||||
it('makes changes to unbound object invisible to other object', function() {
|
||||
// initial state
|
||||
expect(o.get('k')).to.eql(1);
|
||||
expect(o2.get('k')).to.eql(1);
|
||||
o2.unbind('k');
|
||||
expect(o.get('k')).to.eql(1);
|
||||
expect(o2.get('k')).to.eql(1);
|
||||
o2.set('k', 2);
|
||||
expect(o.get('k')).to.eql(1);
|
||||
expect(o2.get('k')).to.eql(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('unbindAll', function() {
|
||||
var o2;
|
||||
|
||||
beforeEach(function() {
|
||||
o2 = new ol.Object();
|
||||
o2.bindTo('k', o);
|
||||
o2.set('k', 1);
|
||||
});
|
||||
|
||||
it('makes changes to unbound object invisible to other object', function() {
|
||||
// initial state
|
||||
expect(o.get('k')).to.eql(1);
|
||||
expect(o2.get('k')).to.eql(1);
|
||||
o2.unbindAll();
|
||||
expect(o.get('k')).to.eql(1);
|
||||
expect(o2.get('k')).to.eql(1);
|
||||
o2.set('k', 2);
|
||||
expect(o.get('k')).to.eql(1);
|
||||
expect(o2.get('k')).to.eql(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bind rename', function() {
|
||||
var listener1, o2, listener2;
|
||||
|
||||
beforeEach(function() {
|
||||
o2 = new ol.Object();
|
||||
o2.bindTo('k2', o, 'k1');
|
||||
|
||||
listener1 = sinon.spy();
|
||||
goog.events.listen(o, 'change:k1', listener1);
|
||||
|
||||
listener2 = sinon.spy();
|
||||
goog.events.listen(o2, 'change:k2', listener2);
|
||||
});
|
||||
|
||||
it('sets the expected properties', function() {
|
||||
o.set('k1', 1);
|
||||
expect(o.get('k1')).to.eql(1);
|
||||
expect(o.get('k2')).to.be(undefined);
|
||||
expect(o2.get('k2')).to.eql(1);
|
||||
expect(o2.get('k1')).to.be(undefined);
|
||||
expect(listener1).to.be.called();
|
||||
expect(listener2).to.be.called();
|
||||
|
||||
expect(o.getKeys()).to.eql(['k1']);
|
||||
expect(o2.getKeys()).to.eql(['k2']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('transitive bind', function() {
|
||||
var o2, o3;
|
||||
|
||||
beforeEach(function() {
|
||||
o2 = new ol.Object();
|
||||
o3 = new ol.Object();
|
||||
o2.bindTo('k2', o, 'k1');
|
||||
o3.bindTo('k3', o2, 'k2');
|
||||
});
|
||||
|
||||
it('sets the expected properties', function() {
|
||||
o.set('k1', 1);
|
||||
expect(o.get('k1')).to.eql(1);
|
||||
expect(o2.get('k2')).to.eql(1);
|
||||
expect(o3.get('k3')).to.eql(1);
|
||||
|
||||
expect(o.getKeys()).to.eql(['k1']);
|
||||
expect(o2.getKeys()).to.eql(['k2']);
|
||||
expect(o3.getKeys()).to.eql(['k3']);
|
||||
});
|
||||
|
||||
describe('backward', function() {
|
||||
|
||||
it('sets the expected properties', function() {
|
||||
o3.set('k3', 1);
|
||||
expect(o.get('k1')).to.eql(1);
|
||||
expect(o2.get('k2')).to.eql(1);
|
||||
expect(o3.get('k3')).to.eql(1);
|
||||
|
||||
expect(o.getKeys()).to.eql(['k1']);
|
||||
expect(o2.getKeys()).to.eql(['k2']);
|
||||
expect(o3.getKeys()).to.eql(['k3']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('circular bind', function() {
|
||||
var o2;
|
||||
|
||||
beforeEach(function() {
|
||||
o2 = new ol.Object();
|
||||
o.bindTo('k', o2);
|
||||
});
|
||||
|
||||
it('throws an error', function() {
|
||||
expect(function() { o2.bindTo('k', o); }).to.throwException();
|
||||
});
|
||||
});
|
||||
|
||||
describe('priority', function() {
|
||||
var o2;
|
||||
|
||||
beforeEach(function() {
|
||||
o2 = new ol.Object();
|
||||
});
|
||||
|
||||
it('respects set order', function() {
|
||||
o.set('k', 1);
|
||||
o2.set('k', 2);
|
||||
o.bindTo('k', o2);
|
||||
expect(o.get('k')).to.eql(2);
|
||||
expect(o2.get('k')).to.eql(2);
|
||||
});
|
||||
|
||||
it('respects set order (undefined)', function() {
|
||||
o.set('k', 1);
|
||||
o.bindTo('k', o2);
|
||||
expect(o.get('k')).to.be(undefined);
|
||||
expect(o2.get('k')).to.be(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setter', function() {
|
||||
@@ -452,27 +178,12 @@ describe('ol.Object', function() {
|
||||
sinon.spy(o, 'setX');
|
||||
});
|
||||
|
||||
describe('without bind', function() {
|
||||
it('does not call the setter', function() {
|
||||
o.set('x', 1);
|
||||
expect(o.get('x')).to.eql(1);
|
||||
expect(o.setX).to.not.be.called();
|
||||
it('does not call the setter', function() {
|
||||
o.set('x', 1);
|
||||
expect(o.get('x')).to.eql(1);
|
||||
expect(o.setX).to.not.be.called();
|
||||
|
||||
expect(o.getKeys()).to.eql(['x']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with bind', function() {
|
||||
it('does call the setter', function() {
|
||||
var o2 = new ol.Object();
|
||||
o2.bindTo('x', o);
|
||||
o2.set('x', 1);
|
||||
expect(o.setX).to.be.called();
|
||||
expect(o.get('x')).to.eql(1);
|
||||
|
||||
expect(o.getKeys()).to.eql(['x']);
|
||||
expect(o2.getKeys()).to.eql(['x']);
|
||||
});
|
||||
expect(o.getKeys()).to.eql(['x']);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -484,29 +195,9 @@ describe('ol.Object', function() {
|
||||
sinon.spy(o, 'getX');
|
||||
});
|
||||
|
||||
describe('without bind', function() {
|
||||
it('does not call the getter', function() {
|
||||
expect(o.get('x')).to.be(undefined);
|
||||
expect(o.getX).to.not.be.called();
|
||||
});
|
||||
});
|
||||
|
||||
describe('with bind', function() {
|
||||
it('does call the getter', function() {
|
||||
var o2 = new ol.Object();
|
||||
o2.bindTo('x', o);
|
||||
expect(o2.get('x')).to.eql(1);
|
||||
expect(o.getX).to.be.called();
|
||||
|
||||
expect(o.getKeys()).to.eql([]);
|
||||
expect(o2.getKeys()).to.eql(['x']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bind self', function() {
|
||||
it('throws an error', function() {
|
||||
expect(function() { o.bindTo('k', o); }).to.throwException();
|
||||
it('does not call the getter', function() {
|
||||
expect(o.get('x')).to.be(undefined);
|
||||
expect(o.getX).to.not.be.called();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -538,130 +229,6 @@ describe('ol.Object', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('transforms', function() {
|
||||
|
||||
describe('original states and events', function() {
|
||||
it('bindTo and transform emit propertychange events', function() {
|
||||
var source = new ol.Object();
|
||||
var target = new ol.Object();
|
||||
source.set('x', 1);
|
||||
target.set('x', 2);
|
||||
var sourceSpy = sinon.spy();
|
||||
var targetSpy = sinon.spy();
|
||||
source.on('propertychange', sourceSpy);
|
||||
target.on('propertychange', targetSpy);
|
||||
var accessor = source.bindTo('x', target);
|
||||
expect(sourceSpy.callCount).to.be(1);
|
||||
expect(targetSpy.callCount).to.be(0);
|
||||
expect(source.get('x')).to.be(2);
|
||||
expect(target.get('x')).to.be(2);
|
||||
accessor.transform(function(v) {
|
||||
return v * 2;
|
||||
}, function(v) {
|
||||
return v / 2;
|
||||
});
|
||||
var call, args;
|
||||
expect(sourceSpy.calledTwice).to.be(true);
|
||||
call = sourceSpy.firstCall;
|
||||
expect(call.args).to.have.length(1);
|
||||
expect(call.args[0].key).to.be('x');
|
||||
expect(call.args[0].oldValue).to.be(1);
|
||||
call = sourceSpy.secondCall;
|
||||
expect(call.args).to.have.length(1);
|
||||
expect(call.args[0].key).to.be('x');
|
||||
expect(call.args[0].oldValue).to.be(2);
|
||||
expect(targetSpy.called).to.be(false);
|
||||
expect(source.get('x')).to.be(1);
|
||||
expect(target.get('x')).to.be(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with multiple binds to a single property', function() {
|
||||
|
||||
var original, plusOne, asString;
|
||||
|
||||
beforeEach(function() {
|
||||
original = new ol.Object();
|
||||
original.set('x', 1);
|
||||
plusOne = new ol.Object();
|
||||
plusOne.bindTo('x', original).transform(
|
||||
function(x) { return x - 1; },
|
||||
function(x) { return x + 1; }
|
||||
);
|
||||
asString = new ol.Object();
|
||||
asString.bindTo('x', original).transform(
|
||||
function(x) { return +x; },
|
||||
function(x) { return x + ''; }
|
||||
);
|
||||
});
|
||||
|
||||
it('returns the expected value', function() {
|
||||
expect(original.get('x')).to.be(1);
|
||||
expect(plusOne.get('x')).to.be(2);
|
||||
expect(asString.get('x')).to.be('1');
|
||||
});
|
||||
|
||||
it('allows the original value to be set correctly', function() {
|
||||
original.set('x', 2);
|
||||
expect(plusOne.get('x')).to.be(3);
|
||||
expect(asString.get('x')).to.be('2');
|
||||
});
|
||||
|
||||
it('allows the transformed values to be set correctly', function() {
|
||||
plusOne.set('x', 3);
|
||||
expect(original.get('x')).to.be(2);
|
||||
expect(asString.get('x')).to.be('2');
|
||||
asString.set('x', '3');
|
||||
expect(original.get('x')).to.be(3);
|
||||
expect(plusOne.get('x')).to.be(4);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('with transitive binds', function() {
|
||||
|
||||
var original, plusOne, plusOneAsString;
|
||||
|
||||
beforeEach(function() {
|
||||
original = new ol.Object();
|
||||
original.set('x', 1);
|
||||
plusOne = new ol.Object();
|
||||
plusOne.bindTo('x', original).transform(
|
||||
function(x) { return x - 1; },
|
||||
function(x) { return x + 1; }
|
||||
);
|
||||
plusOneAsString = new ol.Object();
|
||||
plusOneAsString.bindTo('x', plusOne).transform(
|
||||
parseFloat,
|
||||
function(x) { return x + ''; }
|
||||
);
|
||||
});
|
||||
|
||||
it('returns the expected value', function() {
|
||||
expect(original.get('x')).to.be(1);
|
||||
expect(plusOne.get('x')).to.be(2);
|
||||
expect(plusOneAsString.get('x')).to.be('2');
|
||||
});
|
||||
|
||||
it('allows the original value to be set correctly', function() {
|
||||
original.set('x', 2);
|
||||
expect(plusOne.get('x')).to.be(3);
|
||||
expect(plusOneAsString.get('x')).to.be('3');
|
||||
});
|
||||
|
||||
it('allows the transformed values to be set correctly', function() {
|
||||
plusOne.set('x', 3);
|
||||
expect(original.get('x')).to.be(2);
|
||||
expect(plusOneAsString.get('x')).to.be('3');
|
||||
plusOneAsString.set('x', '4');
|
||||
expect(original.get('x')).to.be(3);
|
||||
expect(plusOne.get('x')).to.be(4);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user