6

One can kick an observable calling its valueHasMutated method to force a notification to the subscribers. What does valueWillMutate do?

Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
  • 2
    Why don't you check it out in the source code: https://github.com/knockout/knockout/search?q=valueWillMutate&ref=cmdform? – nemesv Jul 31 '14 at 14:06
  • Maybe `valueBeforeMutate` would have been a more suitable name – GôTô Jul 31 '14 at 14:26
  • That name sounds more like a property to get the value from before the last mutation, or something. valueWillMutate happens to trigger an event that passes along the value before mutation, but the function itself has a different responsibility. – Hans Roerdinkholder Aug 01 '14 at 11:14
  • @nemesv because i have SO, where i can get a better answer from a human – Trident D'Gao May 22 '17 at 16:20

1 Answers1

9

When subscribing to an observable, sometimes you want to know the previous value of the observable when it has changed. For example, when you have an observable selectedItem (in a list of items), but each individual item also has a selected property. When selectedItem changes, you want to selected selected = false on the previously selected item. You can do that like this:

selectedItem.subscribe(function (previous) {
    previous.selected = false;
}, null, 'beforeChange');

valueWillMutate is used to trigger the beforeChange event.

Edit: for even more utility, take a look at Knockout-2.2.0, subscribe get value before change AND new value. The highest-voted answer creates an extender that allows you to subscribe to an observable and use both the old and new value at the same time.

Edit 2: Just to clarify: you do not need to explicity call valueWillMutate to get the beforeChange event: Knockout does that for you when you operate on an observable. You only need to do it manually when you operate on the underlying value, or when you want to explicity trigger subscribers for some reason. My answer was written from the point of view of Knockout's internal implementation, which I didn't really make clear.

Community
  • 1
  • 1
Hans Roerdinkholder
  • 3,000
  • 1
  • 20
  • 30
  • 1
    Typically, a `beforeChange` event will only fire when an observable is in the process of actually being changed. A direct call to `valueWillMutate` will allow you get that `beforeChange` event to fire without actually changing the observable. To use the OP's own words... it allows you to "kick" the observable's `beforeChange` event. – 300 baud Jul 31 '14 at 14:42
  • I see what you are saying, and that would indeed be correct if it is only used by the end user. But internally Knockout uses the valueWillMutate function a lot. Also, if you operate on the underlying value of an observable, the `beforeChange` event will not fire. In this case, you have to do it yourself. Operating on the underlying value often makes sense from a performance aspect, especially on observableArrays. Nevertheless your comment is a useful addition to my answer, as my answer might imply to the reader that you always have to call valueWillMutate yourself to get the beforeChange event – Hans Roerdinkholder Jul 31 '14 at 14:45
  • 1
    I certainly wasn't trying to dispute your answer. I was just trying to re-enforce it by putting it in slightly different terms. That is why I added it as a comment to your answer rather than as a standalone answer. :) – 300 baud Jul 31 '14 at 15:05
  • And I'm glad you did. The answer is more complete now. Thanks :) – Hans Roerdinkholder Aug 01 '14 at 07:12