0

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

如何让绑定工作?我做错什么了吗?

输入.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;
});

表单.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;
});

模块.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这不起作用。

<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这确实有效,但我没有为 foreach 使用 classBindingProvider。

<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>

没有错误消息,但绑定永远不会发生。我只得到 3 个空输入字段。

4

1 回答 1

0

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

我改变了两件事。首先,我添加<div data-class="Inputs.Item">并引用了相对于该位置(InputTest)的属性。其次,我立即在getBindings函数中注册绑定,现在将它们转换为initBindings.

输入.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;
});

表单.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;
});

模块.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;
});

模块.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>
于 2016-08-18T22:54:03.910 回答