KnockoutClassBindingProvider:如何执行foreach绑定

时间:2021-04-01 20:34:24

I see the basic example on github but I can't get it to work with my code. I should add that I'm using durandal.

我在github上看到了基本的例子,但我无法使用我的代码。我应该补充一点,我正在使用durandal。

How do I get the bindings to work? Am I doing anything wrong?

如何使绑定工作?我做错了吗?

Input.js

define(['knockout'], function (ko) {

    var ctor = function (value) {
        //Properties
        this.value = ko.observable(value);
        this.placeholder = 'Input';

        //Methods
        this.getBindings = function () {
            var bindings = {};
            bindings.Input = {
                value: this.value,
                attr: {
                    placeholder: this.placholder,
                },
            };
            bindings.Test = {
                text: this.value,
            };

           return bindings;
        };
    };


    return ctor;
});

Form.js

define(['knockout', 'Input'], function (ko, Input) {

    var ctor = function (inputs) {
        //Properties
        this.inputs = ko.observableArray(inputs);

        //Methods
        this.getBindings = function () {
            var bindings = {};
            bindings.Inputs = {
                foreach: this.inputs,
                Item: function (context, classes) {
                    return context.$data.getBindings();
                },
            };

            return bindings;
        };
    };


    return ctor;
});

Module.js

define(['knockout', 'Input', 'Form'], function (ko, Input, Form) {
    var ctor = function () { };

    ctor.prototype.activate = function () {
        var data = [
            new Input(123),
            new Input("Chris"),
            new Input(true)
        ];
        this.form = new Form(data);
    };

    ctor.prototype.binding = function () {
        var bindings = this.form.getBindings();
        ko.bindingProvider.instance.registerBindings(bindings);
    };


    return ctor;
});

Module.html This does not work.

Module.html这不起作用。

<div id="Module">
    <div data-class="Inputs">
        <div>
            <input data-class="Inputs.Item.Input" />
            <span data-class="Inputs.Item.Test"></span>
        </div>
    </div>
</div>

Module.html This does work but I'm not using classBindingProvider for the foreach.

Module.html这确实有效,但我没有使用classBindingProvider作为foreach。

<div id="Module">
    <div data-class="Inputs">
        <div>
            <input data-bind="value: value, attr: { placeholder: placeholder }" />
            <span data-bind="text: value"></span>
        </div>
    </div>
</div>

There's no error message but the binding never happens. I just get 3 empty input fields.

没有错误消息,但绑定从未发生过。我只得到3个空输入字段。

1 个解决方案

#1


0  

I figured it out. I'll post the code that works.

我想到了。我会发布有效的代码。

I changed two things. First, I added <div data-class="Inputs.Item"> and then referenced the properties relative to that location (Input and Test). Second, I register the bindings immediately inside the getBindings functions, which will now turn them into initBindings.

我改变了两件事。首先,我添加了

,然后引用了相对于该位置的属性(输入和测试)。其次,我在getBindings函数中立即注册绑定,现在将它们转换为initBindings。

Input.js

define(['knockout'], function (ko) {

    var ctor = function (value) {
        //Properties
        this.value = ko.observable(value);
        this.placeholder = 'Input';

        //Methods
        this.initBindings = function () { //FIX: getBindings => initBindings
            var bindings = {};
            bindings.Input = {
                value: this.value,
                attr: {
                    placeholder: this.placholder,
                },
            };
            bindings.Test = {
                text: this.value,
            };

            ko.bindingProvider.instance.registerBindings(bindings); //FIX: register instead of return
        };
    };

    return ctor;
});

Form.js

define(['knockout', 'Input'], function (ko, Input) {

    var ctor = function (inputs) {
        //Properties
        this.inputs = ko.observableArray(inputs);

        //Methods
        this.initBindings = function () { //FIX: getBindings => initBindings
            var bindings = {};
            bindings.Inputs = {
                foreach: this.inputs,
                Item: function (context, classes) {
                    context.$data.initBindings(); //FIX: Call the init.
                },
            };

            ko.bindingProvider.instance.registerBindings(bindings); //FIX: register instead of return
        };
    };

    return ctor;
});

Module.js

define(['knockout', 'Input', 'Form'], function (ko, Input, Form) {
    var ctor = function () { };

    ctor.prototype.activate = function () {
        var data = [
            new Input(123),
            new Input("Chris"),
            new Input(true)
        ];
        this.form = new Form(data);
    };

    ctor.prototype.binding = function () {
        this.form.initBindings(); //FIX: Call the init.
    };

    return ctor;
});

Module.html

<div id="Module">
    <div data-class="Inputs">
        <div data-class="Inputs.Item"> //FIX: no binding => Inputs.Item
            <input data-class="Input" /> //FIX: Inputs.Item.Input => Input
            <span data-class="Test"> //Fix: Inputs.Item.Test => Test
            </span> 
        </div>
    </div>
</div>

#1


0  

I figured it out. I'll post the code that works.

我想到了。我会发布有效的代码。

I changed two things. First, I added <div data-class="Inputs.Item"> and then referenced the properties relative to that location (Input and Test). Second, I register the bindings immediately inside the getBindings functions, which will now turn them into initBindings.

我改变了两件事。首先,我添加了

,然后引用了相对于该位置的属性(输入和测试)。其次,我在getBindings函数中立即注册绑定,现在将它们转换为initBindings。

Input.js

define(['knockout'], function (ko) {

    var ctor = function (value) {
        //Properties
        this.value = ko.observable(value);
        this.placeholder = 'Input';

        //Methods
        this.initBindings = function () { //FIX: getBindings => initBindings
            var bindings = {};
            bindings.Input = {
                value: this.value,
                attr: {
                    placeholder: this.placholder,
                },
            };
            bindings.Test = {
                text: this.value,
            };

            ko.bindingProvider.instance.registerBindings(bindings); //FIX: register instead of return
        };
    };

    return ctor;
});

Form.js

define(['knockout', 'Input'], function (ko, Input) {

    var ctor = function (inputs) {
        //Properties
        this.inputs = ko.observableArray(inputs);

        //Methods
        this.initBindings = function () { //FIX: getBindings => initBindings
            var bindings = {};
            bindings.Inputs = {
                foreach: this.inputs,
                Item: function (context, classes) {
                    context.$data.initBindings(); //FIX: Call the init.
                },
            };

            ko.bindingProvider.instance.registerBindings(bindings); //FIX: register instead of return
        };
    };

    return ctor;
});

Module.js

define(['knockout', 'Input', 'Form'], function (ko, Input, Form) {
    var ctor = function () { };

    ctor.prototype.activate = function () {
        var data = [
            new Input(123),
            new Input("Chris"),
            new Input(true)
        ];
        this.form = new Form(data);
    };

    ctor.prototype.binding = function () {
        this.form.initBindings(); //FIX: Call the init.
    };

    return ctor;
});

Module.html

<div id="Module">
    <div data-class="Inputs">
        <div data-class="Inputs.Item"> //FIX: no binding => Inputs.Item
            <input data-class="Input" /> //FIX: Inputs.Item.Input => Input
            <span data-class="Test"> //Fix: Inputs.Item.Test => Test
            </span> 
        </div>
    </div>
</div>