Giant Jupiter
Giant Jupiter

Reputation: 11

observable doesn't update custom bind

Please refer to the codes attached.

DummyText is observable and a custom bind defined in ko.bindingHandlers.dummytext. However, this.DummyText("dummy_text_" + this.firstName()); only calls update: function (element, valueAccessor,.... once, so that <span data-bind="dummytext: DummyText"></span> is only updated once.

Text is created as a reference which is text-bind, and <span data-bind="text: Text"></span> is always updated.

How to let this.DummyText("dummy_text_" + this.firstName()); always call update: function (element, valueAccessor,....

<html>
<head>
</head>
    <script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.js"></script>
    <script src="//ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.debug.js"></script>
<body>
    <p>First name: <input data-bind="value: firstName" /></p>
    <p>Last name: <input data-bind="value: lastName" /></p>
    <h2>Hello, <span data-bind="text: fullName"> </span>!</h2>
    <div><span data-bind="text: Text"></span></div>
    <div><span data-bind="dummytext: DummyText"></span></div>

    <script>
        ko.bindingHandlers.dummytext = {
            //init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            //},
            update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
                var value = valueAccessor(), allBindings = allBindingsAccessor();
                var s = value._latestValue;
                $(element).html(s);
            }
        };

        var ViewModel = function (first, last) {
            this.firstName = ko.observable(first);
            this.lastName = last;
            this.DummyText = ko.observable("dummy_text_");
            this.Text = ko.observable("text_");

            this.fullName = ko.computed(function () {
                this.Text("text_" + this.firstName());
                this.DummyText("dummy_text_" + this.firstName());

                return this.firstName() + " " + this.lastName;
            }, this);
        };

        ko.applyBindings(new ViewModel("Planet", "Earth"));
        </script>

</body>
</html>

Upvotes: 1

Views: 266

Answers (1)

Lynn Ning
Lynn Ning

Reputation: 71

You should use value() instead of value._latestValue to trigger the dependency so the Knockout could know current binding needs refresh when the value gets updated:

Check out this: http://jsfiddle.net/b4bDQ/

ko.bindingHandlers.dummytext = {
        //init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        //},
        update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
            var value = valueAccessor(), allBindings = allBindingsAccessor();
            var s = value(); // Here should use () to trigger the subcribing dependency // value._latestValue;
            $(element).html(s);
        }
    };

Upvotes: 2

Related Questions