Exemple #1
0
        it("localizes the messages when other data bindings update", function() {
            message.key = "Hello, {name}";

            var otherObject = {
                name: "before"
            };

            var object = {};

            Bindings.defineBinding(object, "name", {
                "<->": "name",
                source: otherObject,
            });

            Bindings.defineBinding(message, "data.get('name')", {
                "<->": "name",
                source: object,
            });

            return message.localized.then(function (localized) {
                expect(localized).toBe("Hello, before");
                otherObject.name = "after";

                return message.localized;
            }).then(function (localized) {
                expect(localized).toBe("Hello, after");
            });
        });
Exemple #2
0
        it("should serialize a binding to a shorthand format", function () {
            var Alpha = Montage.specialize( {foo: {value: null}}),
                Omega = Montage.specialize( {bar: {value: null}}),
                target = new Alpha(),
                source = new Omega(),
                serializer = new Serializer().initWithRequire(require),
                serialization,
                expectedSerialization = {
                    "root": {
                        "prototype": "montage/core/core[Montage]",
                        "properties": {
                            "foo": null,
                            "identifier": null
                        },
                        "bindings": {
                            "foo": {
                                "<-": "@montage.bar"
                            }
                        }
                    },
                    "montage": {}
                };

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<-": "bar"
            });

            serialization = serializer.serializeObject(target);
            expect(JSON.parse(serialization)).toEqual(expectedSerialization);
        });
Exemple #3
0
        it("should call \"bindings\" deserialization unit", function () {
            var Alpha = Montage.specialize( {foo: {value: null}}),
                Omega = Montage.specialize( {bar: {value: null}}),
                target = new Alpha(),
                source = new Omega(),
                serializer = new Serializer().initWithRequire(require),
                deserializer = new Deserializer();

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<-": "bar"
            });

            var serialization = serializer.serializeObject(target);
            var labels = {};

            labels["root"] = source;
            labels.montage = {};
            deserializer.init(serialization, require);
            spyOn(MontageReviver._unitRevivers, "bindings").andCallThrough();

            return deserializer.deserialize(labels)
            .then(function (objects) {
                object = objects.root;
                expect(MontageReviver._unitRevivers.bindings).toHaveBeenCalled();
            });
        });
Exemple #4
0
        it("should not go out of its way to protect you from mutations to an object on the left making their way over to the right", function () {

            var target = new Alpha(),
            source = new Omega();

            source.bar = ["a", "b", "c"];

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<-": "bar"
            });

            // Ideally, this should be avoided; but the way it works is expected
            target.foo.push("d");

            expect(target.foo.length).toBe(4);
            expect(target.foo[0]).toBe("a");
            expect(target.foo[1]).toBe("b");
            expect(target.foo[2]).toBe("c");
            expect(target.foo[3]).toBe("d");

            expect(source.bar.length).toBe(4); // Yep, this is a little unexpected
            expect(source.bar[0]).toBe("a");
            expect(source.bar[1]).toBe("b");
            expect(source.bar[2]).toBe("c");
            expect(source.bar[3]).toBe("d"); //but it makes sense, left and right are the same array
        });
Exemple #5
0
        it ("should affect and emit changes on both sides of a two-way binding", function () {
            var target = new Alpha(),
                source = new Omega();

            var myArray = ["a", "b", "c"];

            source.bar = myArray;

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<->": "bar.rangeContent()"
            });

            var changeListener = {
                targetListener: function () {
                    expect(target.foo.length).toBe(4);
                    expect(target.foo[3]).toBe("d");
                },
                sourceListener: function () {
                    expect(source.bar.length).toBe(4);
                    expect(source.bar[3]).toBe("d");
                }
            };

            spyOn(changeListener, "targetListener").andCallThrough();
            spyOn(changeListener, "sourceListener").andCallThrough();

            target.foo.addRangeChangeListener(changeListener.targetListener, false);
            source.bar.addRangeChangeListener(changeListener.sourceListener, false);

            myArray.push("d");

            expect(changeListener.targetListener).toHaveBeenCalled();
            expect(changeListener.sourceListener).toHaveBeenCalled();
        });
Exemple #6
0
            it ("should propagate a change at the bound property as a 'converted' value to the source property", function() {
                Bindings.defineBinding(theObject, "foo", bindingDescriptor);
                theObject.bar = "no";

                expect(theObject.foo).toBe(false);
                expect(theObject.bar).toBe("no");
            });
Exemple #7
0
        it ("must not propagate a change at the source property to the bound property", function() {
            Bindings.defineBinding(theObject, "foo", bindingDescriptor);
            theObject.foo = "new foo value";

            expect(theObject.bar).toBe("yes");
            expect(theObject.foo).toBe("new foo value");
        });
Exemple #8
0
            it ("should propagate a change at the source property as a 'reverted' value to the bound property the first time such a change occurs", function() {
                Bindings.defineBinding(theObject, "foo", bindingDescriptor);
                theObject.foo = false;

                expect(theObject.foo).toBe(false);
                expect(theObject.bar).toBe("no");
            });
Exemple #9
0
            it ("should propagate a change at the bound property as a 'converted' value to the source property if this change is not the first time the binding is fired", function() {
                Bindings.defineBinding(theObject, "foo", bindingDescriptor);
                theObject.bar = "no";
                theObject.bar = "yes";

                expect(theObject.foo).toBe(true);
                expect(theObject.bar).toBe("yes");
            });
Exemple #10
0
        it("should probably not bother observing an immutable string for a change at its length property", function () {
            var target = new Alpha(),
                source = "test";

                Bindings.defineBinding(target, "foo", {
                    source: source,
                    "<-": "length"
                });

                expect(target.foo).toBe(4);
        });
Exemple #11
0
            beforeEach(function () {

                FormatBar = Montage.specialize( {boldMode: {value: null}});
                DocumentController = Montage.specialize( {boldMode: {value: null}});
                TextItem = Montage.specialize( {boldMode: {value: null}});
                formatBar = new FormatBar();
                documentController = new DocumentController();
                textItem = new TextItem();

                // [formatBar] <====> [DocumentController] ----> [TextItem]
                Bindings.defineBinding(formatBar, "boldMode", {
                    source: documentController,
                    "<->": "boldMode"
                });

                Bindings.defineBinding(textItem, "boldMode", {
                    source: documentController,
                    "<-": "boldMode"
                });
            });
Exemple #12
0
        it("should propagate changes to other objects bound ", function () {
            var a = new Alpha(),
            b = new Omega(),
            c = new Alpha();

            a.foo = ["a", "b", "c"];

            Bindings.defineBinding(b, "bar", {
                source: a,
                "<->": "foo"
            });

            Bindings.defineBinding(c , "foo", {
                source: b,
                "<->": "bar"
            });

            a.foo = ["aa", "bb"];
            expect(b.bar[0]).toBe("aa");
            expect(c.foo[0]).toBe("aa");
        });
Exemple #13
0
        it("should return undefined when defining a binding along a propertyPath that encounters an undefined property", function () {
            var target = new Alpha(),
                source = new Omega();

            source.bar = 42;

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<-": "foo" // the source has no own foo property
            });

            expect(target.foo).toBe(undefined);
        });
Exemple #14
0
        it("should propagate the original value from the bound object's bound property path to the source object's source property path", function () {
            var target = new Alpha(),
            source = new Omega();

            source.bar = "foo";

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<-": "bar"
            });

            expect(target.foo).toBe("foo");
        });
Exemple #15
0
                it("should propagate the addition of a true value to an otherwise false array", function () {
                    var target = new Alpha(),
                    source = new Omega();

                    source.bar = [{a: {b: 0}}, {a: {b: false}}, {a: {b: null}}];

                    Bindings.defineBinding(target, "foo", {
                        source: source,
                        "<-": "bar.some{a.b}"
                    });

                    source.bar.push({a: {b: true}});
                    expect(target.foo).toBe(true);
                });
Exemple #16
0
                it("should propagate a change from true to false", function () {
                    var target = new Alpha(),
                    source = new Omega();

                    source.bar = [{a: {b: 0}}, {a: {b: false}}, {a: {b: true}}];

                    Bindings.defineBinding(target, "foo", {
                        source: source,
                        "<-": "bar.some{a.b}"
                    });

                    source.bar.set(2, {a: null});
                    expect(target.foo).toBe(false);
                });
Exemple #17
0
                it("should propagate the removal of a true value from an otherwise false array", function () {
                    var target = new Alpha(),
                    source = new Omega();

                    source.bar = [0, false, null, true];

                    Bindings.defineBinding(target, "foo", {
                        source: source,
                        "<-": "bar.some{}"
                    });

                    source.bar.pop();
                    expect(target.foo).toBe(false);
                });
Exemple #18
0
        it("should propagate a change from the bound array index contents to the source propertyPath using set when the array changes", function () {
            var target = new Alpha(),
            source = new Omega();

            source.bar = ["a", "b", "c"];

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<->": "bar.0"
            });

            source.bar = ["aa", "bb"];
            expect(target.foo).toBe("aa");
        });
Exemple #19
0
                it("should propagate a change from true to false", function () {
                    var target = new Alpha(),
                    source = new Omega();

                    source.bar = [0, false, true];

                    Bindings.defineBinding(target, "foo", {
                        source: source,
                        "<-": "bar.some{}"
                    });

                    source.bar.set(2, null);
                    expect(target.foo).toBe(false);
                });
Exemple #20
0
        it("should propagate a change from the bound object when the property path includes an array index and that element is removed", function () {
            var target = new Alpha(),
                    source = new Omega();

            source.bar = [{x: 1}];

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<->": "bar.0.x"
            });

            source.bar.pop();
            expect(target.foo).toBeUndefined();
        });
Exemple #21
0
            it("should work as expected", function () {
                var target = new Alpha();

                target.valueOnly = "startValue";

                Bindings.defineBinding(target, "foo", {
                    source: target,
                    "<-": "valueOnly"
                });

                expect(target.foo).toBe("startValue");

                target.valueOnly = "newValue!";
                expect(target.foo).toBe("newValue!");
            });
Exemple #22
0
        it("should correctly observe a nonexistent index", function () {
            var target = new Alpha(),
                source = new Omega();

            source.bar = [];

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<-": "bar.1000" //this index does not exist yet
            });

            source.bar.set(1000, 42);

            expect(target.foo).toBe(42);
        });
Exemple #23
0
        it("should properly install a binding on a property that has only a 'get' in its propertyDescriptor, without making the property 'set-able'", function () {
            var target = new Omega(),
                source = new Alpha();

            Bindings.defineBinding(target, "bar", {
                source: source,
                "<-": "getOnly"
            });

            expect(target.bar).toBe("getOnlyValue");

            source.getOnly = "newGetOnlyValue";
            expect(source.getOnly).toBe("getOnlyValue");
            expect(target.bar).toBe("getOnlyValue");
        });
Exemple #24
0
        it("should correctly observe the length property of an object if the object at that property was previously a string", function () {
            var target = new Alpha(),
                source = new Alpha();

                source.bar = "test";

                Bindings.defineBinding(target, "foo", {
                    source: source,
                    "<-": "bar.length"
                });

                source.bar = {length: 12, units: "inches"};

                expect(target.foo).toBe(12);
        });
Exemple #25
0
        it("should propagate a change to a string found at a property when the string's length is being observed", function () {
            var target = new Alpha(),
                source = new Alpha();

                source.bar = "test";

                Bindings.defineBinding(target, "foo", {
                    source: source,
                    "<-": "bar.length"
                });

                source.bar = "hello world";

                expect(target.foo).toBe(11);
        });
Exemple #26
0
                it("should propagate a change from false to true when a false object is pushed to a path after establishing the binding, and then changed to true", function () {
                    var target = new Alpha(),
                    source = new Omega();

                    var myObj = {a: 0};
                    source.bar = [{a: false}, {a: null}];

                    Bindings.defineBinding(target, "foo", {
                        source: source,
                        "<-": "bar.some{a}"
                    });

                    source.bar.push(myObj);
                    myObj.a = true;
                    expect(target.foo).toBe(true);
                });
Exemple #27
0
        set: function(value) {
            if (this._contentController === value) {
                return;
            }

            this._contentController = value;
            value.multiSelect = this.multiple;

            Bindings.defineBindings(this, {
                "content": {"<-": "_contentController.organizedContent"},
                "_selection": {"<-": "_contentController.selection"},
                "_selectedIndexes.rangeContent()": {
                    "<-": "content.enumerate().filter{$_selection.has(.1)}.map{.0}"
                }
            });
        }
Exemple #28
0
        it("should remove a binding on an multiple level property path", function () {
            var target = new Alpha(),
            source = new Omega();

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<->": "bar.x"
            });

            source.bar = {x: 1};
            expect(target.foo).toBe(1);
            Object.deleteBinding(target, "foo");

            source.bar = {x: 2};
            expect(target.foo).toBe(1);
        });
Exemple #29
0
        it("should remove a binding on an property path that includes an array index", function () {
            var target = new Alpha(),
            source = new Omega();

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<->": "bar.0.x"
            });

            source.bar = [{x: 1}];
            expect(target.foo).toBe(1);
            Object.deleteBinding(target, "foo");

            source.bar = [{x: 2}];
            expect(target.foo).toBe(1);
        });
Exemple #30
0
        it("should give the specified converter a chance to modify the value being passed from the source object to the bound object", function () {
            var target = new Alpha(),
                    source = new Omega();

            Bindings.defineBinding(target, "foo", {
                source: source,
                "<-": "bar",
                convert: function () {
                    return "yay";
                }
            });

            source.bar = "foo";

            expect(target.foo).toBe("yay");
        });