Js knockout calculated does not work in model

I am trying to change the following jockout model binding for my requirement: http://jsfiddle.net/zrBuL/291/ . The modification is as follows:

HTML:

<label>Male
   <input type="radio" name="IsMale" value="a" data-bind="checked:IsMale"/>
</label> 
<label>Female
   <input type="radio" name="IsMale" value="b" data-bind="checked:IsMale"/>
</label>
<div data-bind="text: ABC()"/>

JavaScript:

var vm = {
    IsMale: ko.observable(false),
    ABC:ko.purelyComputed({
    read: function(){
            return this.IsMale();
        }
    },this)  
};
ko.applyBindings(vm);

For the framework, I would prefer knockout.js 3.0.0.

The problem is that it does not display anything for the div above where the div text property is bound to ABC ().

Hint: if I replace the following line in the read function:

return this.IsMale();

with the following line:

return "Hi";

then it works like a charm.

Is there something I am missing when calling the IsMale property?

+4
source share
2 answers

purelyComputed pureComputed Knockout 3.2.0 . this window.

, vm window, function :

function MyVM() {
    this.IsMale = ko.observable(false);
    this.ABC = ko.pureComputed({
    read: function(){
            return this.IsMale();
        }
    },this);
};
ko.applyBindings(new MyVM());

.

:

var vm = {
    IsMale: ko.observable(false)
};

vm.ABC = ko.pureComputed({
    read: function(){
        return this.IsMale();
    }
}, vm);
+1

, :

  • purelyComputed, pureComputed

  • pureComputed, Knockout 3.2.0, , .

+1

Source: https://habr.com/ru/post/1599064/


All Articles