Beginner Knockoutjs Record 8--computed observables dependency monitoring (5 Computed observables Reference Compute Monitoring Property Reference)

Source: Internet
Author: User

Computed observables Reference Calculation Monitoring Property Reference

The following document describes how to construct and use the computed monitoring properties.

Constructing a computed ovservable construction calculation monitoring properties

A computed monitoring property can be constructed in any of the following ways:

    1. ko.computed (evaluator [, TargetObject, Options]) --This is the most common way to create a calculated monitoring attribute that supports most scenarios.

        Evaluator --a function used to evaluate the value of the current computed monitoring property;

        TargetObject --If this option is set, the option defines the value of this for use in KO calling your callback function, see section managing this for more information;

        Options -An object that has more properties to calculate the monitoring properties and participate in the following complete list;

   2. ko.computed (options) --this method of creating a calculated monitoring property has a parameter that can be passed to a JavaScript object with the following properties.

        read-required , a function to evaluate the current value of the computed calculation monitoring property;

        write -optional, if this option is set, it means that a writable computed monitoring attribute is defined. This is a function that accepts other code to pass in the value of the computed monitoring property, which can be determined by you, through the custom logic to process the incoming value, by writing these values to the predecessor monitoring property;

        owner --optional, if set, this option defines the value of this for use in Ko calling your read or write callback function;

        Pure -optional, if the option is set to true, the current calculated monitoring property is set to the pure computed monitoring property, which can be used to replace the ko.purecomputed constructor;

Deferevaluation-Optional, if this option is set to True, the value of the computed monitoring property is evaluated only when the actual view is accessed or manually subscribed. By default, the calculated monitoring property gets a value as soon as it is created;

Diposewhen-Optional, if this option is set, the function will be called every time the evaluation of the Monitoring property garbage collection is evaluated, and the return value of the function if True will trigger the destruction of the computed monitoring property;

diposewhennodeisremoved-Optional, if this option is set, the garbage collection of the computed monitoring property is triggered when the specified DOM element is removed by KO. This feature is used to destroy the corresponding computed monitoring attribute when the node is removed by the template and control flow bindings;

    3. ko.purecomputed (evaluator [, TargetObject]) --constructs a purely computed monitoring property, uses the specified calculation function and an optional option to define this, unlike the kocomputed, which does not support the optional Key parameters;

    4. ko.purecomputed (options) --Using the option object to define the pure calculation monitoring properties, the acceptable options are defined as read, write and owner, and the options are as above;

Using a computed observable to use the computed monitoring properties

The function that calculates the Monitoring property provides:

Dispose ()--Manually destroy the computed monitoring properties to clear all of its dependent subscriptions, which is useful in a number of situations, such as if you want to stop the computed monitoring property update, or need to clean up the computed monitoring properties memory that is still dependent on the dependency;

Extend (extenders)--The computed monitoring attribute accepts the specified extenders extension;

Getdependenciescount ()--Gets the dependent quantity of the current calculated monitoring property;

Getsubscriptionscount ([Event])--Gets the subscription for the current computed monitoring property, whether from another computed monitoring attribute or a manual subscription. You can arbitrarily pass in an event name (for example, "change") to get the number of subscriptions for the event;

IsActive ()--Returns whether the current computed monitoring property is active, that is, whether it can be updated, and when the computed monitoring property does not contain any subscriptions, it will be in inactive idle state;

Peek ()--Returns the current value of the computed monitoring property in the case of not creating dependencies (see Chapter Peek)

Subscribe (callback [, Callbacktarget, event])--Returns a manual subscription to notify the change of the computed monitoring attribute;

Determining the type of the observable type detection monitoring property

The following methods can be used to detect a property that is a monitoring property, to calculate monitoring properties, and so on:

    ko.isobservable -When monitoring an array for monitoring properties, all computed monitoring properties are returned true;

    ko.iswritableobservable --Returns True when monitoring an array for the monitoring property, the writable computed monitoring property (the equivalent function is ko.iswriteableobservable);

    ko.iscomputed -- all computed monitoring properties return true;

    ko.ispurecomputed -Returns True when monitoring attributes for pure computing ;

Using the computed context uses the compute contexts

You can access the information that Ko.computedcontext uses to get the current computed properties when performing an evaluation calculation function that calculates the monitoring properties. It provides the following methods:

Isinitial () returns True if the function is called when the calculation function for the Monitoring property is first executed, otherwise false, isinitial () is always undefined for the purely computed monitoring property;

Getdependenciescount ()--Returns the number of dependencies detected when the current evaluation calculation function is executed;

Note: Ko.computedConext.getDependenciesCount () is equivalent to invoking the Getdependenciescount () of the computed monitoring property itself; It provides a way to obtain a dependent quantity for the first time the evaluation calculation function is evaluated before evaluating the monitoring property completion construct, for example:

varmycomputed = ko.computed (function() {    // ... Omitted:read some data that might be observable ...    //Now let ' s inspect ko.computedcontext    varIsfirstevaluation =ko.computedContext.isInitial (), Dependencycount=Ko.computedContext.getDependenciesCount (), Console.log ("Evaluating" + (isfirstevaluation? ") For the first time ":" Again ")); Console.log ("By now, this computed have" + dependencycount + "dependencies"); // ... Omitted:return The result ...});

These tool functions are usually only useful in advanced scenarios, for example, when your calculation monitoring properties are primarily intended to trigger some incidental effects when evaluating calculations, or if you only want to perform some setup logic when the evaluation calculation is first performed, or if and only if it has only one dependency, most scenarios, Computed properties do not need to be concerned with evaluating whether the calculation functions have been executed or how much dependency they have.


Beginner Knockoutjs Record 8--computed observables dependency monitoring (5 Computed observables Reference Compute Monitoring Property Reference)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.