UNPKG

@danielkalen/simplybind

Version:

Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.

70 lines (51 loc) 2.06 kB
suite "Proxy", ()-> test "The binding will be useless/ineffictive if the target property is not a function", ()-> invokeCount = 0 obj = regProp:'value' SimplyBind('func:regProp').of(obj) .transformSelf (args)-> [args[0]/3, args[1]/3] .to (value)-> invokeCount++ expect(invokeCount).to.equal(0) obj.regProp = ()-> 'someValue' obj.regProp() expect(invokeCount).to.equal(0) test "If the object's property is redefined with another function, the old function will be discarded and the new will be wrapped", ()-> invokeCount = fromSub:0, fromPub:0 obj = base:10, test:(a,b)-> invokeCount.fromPub++; (a+b)*@base SimplyBind('func:test').of(obj).to (value)-> invokeCount.fromSub++ expect(invokeCount.fromPub).to.equal(0) expect(invokeCount.fromSub).to.equal(0) obj.test(2,3) expect(invokeCount.fromPub).to.equal(1) expect(invokeCount.fromSub).to.equal(1) obj.test = obj.test expect(invokeCount.fromPub).to.equal(1) expect(invokeCount.fromSub).to.equal(1) obj.test(20,30) expect(invokeCount.fromPub).to.equal(2) expect(invokeCount.fromSub).to.equal(2) obj.test = (a,b)-> (a+b)*@base expect(invokeCount.fromPub).to.equal(2) expect(invokeCount.fromSub).to.equal(2) obj.test(20,30) expect(invokeCount.fromPub).to.equal(2) expect(invokeCount.fromSub).to.equal(3) obj.test(2,3) expect(invokeCount.fromPub).to.equal(2) expect(invokeCount.fromSub).to.equal(4) test "If the object's property is redefined with another non-function value, the binding will be ineffictive until set with a function value", ()-> obj = base:10, test:(a,b)-> (a+b)*@base invokeCount = 0 SimplyBind('func:test').of(obj).to (value)-> invokeCount++ expect(invokeCount).to.equal(0) origWrappedFn = obj.test origWrappedFn(2,3) expect(invokeCount).to.equal(1) obj.test = 'anotherValue' expect(invokeCount).to.equal(1) origWrappedFn(20,30) expect(invokeCount).to.equal(2) obj.test = origWrappedFn expect(invokeCount).to.equal(2) obj.test(20,30) expect(invokeCount).to.equal(3)