How to access the read value of pureComputed

1

I would like to know how I call the read function. When I write in value: valueView it calls the write function but I want the call to read at the end of the function. Is there a method to warn read?

<input type="text" data-bind="value: valueView" placeholder="VALOR">

    self.valueView = ko.pureComputed({
        read: function () {
            var value = “John”;
            return value;
        },
        write: function (value) {
            callRead() ¿???
        }
    });
    
asked by gvivetapl 16.06.2016 в 09:24
source

1 answer

1

You can directly invoke the name of the observable and this will give you the value (or basically invoke its function get ):

If your pureComputed is called valueView invoke self.valueView() will return 'John' .

Edit the value of the input and you will see how it prints its value in the console.

function App() {
  var self = this;
  self.valueView = ko.pureComputed({
    read: function() {
      var value = 'John';
      console.log('read called');
      return value;
    },
    write: function(value) {
      console.log('>> get value:', self.valueView());
      console.log('passed value:', value);
    }
  });
}

ko.applyBindings(new App());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div id="app">
  <input type="text" data-bind="value: valueView" placeholder="VALOR">
</div>

Notice that you are using a pure computed observable so you should be very careful not to change state when you implement your write method. You should always bear in mind that:

  
  • Evaluating the observable computed does not cause any side effects.

  •   
  • The value of the computed observable does not change based on the number of evaluations or any other hidden information. Its value should be based only on the values of other observables in the application, which for the definition of pure function, its parameters are considered.

  •   

    Update

    Here is an example of the usual cases that NO should be done when working with pureComputed based on the previous points

  • Side effects when evaluating the pureComputed
  • function App() {
      var self = this;
      self.otroValor = 0;
      self.valueView = ko.pureComputed({
        read: function() {
          // ***** Muy mala idea ******
          // No se deben modificar valores en la fase de evaluación
          self.otroValor++;
          // No se deben hacer llamadas en la evaluación
          self.update();
          // No se debe intentar actualizar el elemento actual
          return 'John';
        }
      });
    
      self.update = function() {
        // actualizacion de estado, llamadas al servidor, etc
      }
    }
    
    ko.applyBindings(new App());
    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
    <input type="text" data-bind="value: valueView" placeholder="VALOR">

    The reason for this is that when a pureComputed does not have any subscribers, is sleeping so the code is not executed. For that type of functionality you must use another type of observables.

        
    answered by 16.06.2016 / 15:16
    source