2

Having the following model:

 var dataModel = ko.observable({ someProp: ko.observable() }); var isValid = ko.pureComputed(function () { return dataModel().isValid; }); 

I have the following function:

 function testMe() { dataModel().isValid = false; // This does not work, why? console.log("isValid: " + isValid()); // Doesn't update, shows old value dataModel({ isValid: false }); // This works however I loose all other properties console.log("isValid: " + isValid()); // Prints correctly updated value console.log(ko.toJSON(dataModel())); } 

Whenever I run testMe()

dataModel.isValid = false

and execute

console.log("isValid: " + isValid())

it's still set to "true" even I've set it to false above...why?. The only way I got it to work is to do

dataModel({ isValid: false }); however this way I loose all other properties in my model. How can I make this work?

What am i doing wrong?

2
  • 1
    If I run your code with I do not see "true" in any instance, see this jsfiddle, could you please try to create an "minimal reproducible example" and explain why you expect said results? Commented Sep 8, 2016 at 20:18
  • Also, realize that if a (pure)computed depends on a variable that is not observable, it might not get updated correctly. Commented Sep 8, 2016 at 20:25

1 Answer 1

7

isValid is not an observable. A computed will only know to update if one of its observables has changed. Alternatively, you can tell Knockout that dataModel has changed (which, being an observable that the computed looks at, will cause the computed to recompute) using dataModel.valueHasMutated().

I would recommend using an observable, though. Try something like this:

var dataModel = ko.observable({ someProp: ko.observable(), isValid: ko.observable(true) }); var isValid = ko.pureComputed(function() { return dataModel().isValid(); }); function testMe() { console.log("isValid: " + isValid()); dataModel().isValid(false); console.log("isValid: " + isValid()); console.log(ko.toJSON(dataModel())); } testMe();
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

right on the money. thanks so much!. Also, a question. why does @Jeroen's example works even though isValid is not an observable? jsfiddle.net/8fdyyhr2
@ShaneKm In his example, the value of isValid never changes. It starts false and stays false. That was what his comment was saying.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.