Knockoutjs 3.X API Third Chapter Computing Monitoring properties (5) Reference manual

Source: Internet
Author: User

Calculation Monitoring Property Construction Reference

The calculated monitoring properties can be constructed using the following form:

  1. ko.computed( evaluator [, targetObject, options] )-This form is the most common scenario for creating a computed monitoring attribute.
      • evaluator-function to return a calculated value.
      • targetObject-If a defined value is given, this KO invokes the callback function. See section III, Calculating monitoring properties (1) Use the calculated monitoring properties for more information.
      • options-An object that calculates other properties of the monitoring property. See the complete list below.
  2. ko.computed (options) -the single parameter form used to create a calculation that observes a JavaScript object that accepts any of the following properties.
    • Read -must write. A function to evaluate the current value of the monitoring property.
    • Write -optional. If given, enables the calculation of the monitoring property to writable . This is the function that receives other values and assigns them to the computed monitoring property. This is the place to provide you with custom logic.
    • owner -Optional. If a defined value of this is given, the KO invokes your read or write callback object.
    • Pure -optional. If this option is true , the computed observation will be set to a pure computed observables. This option can be overridden for ko.purecomputed constructs.
    • deferevaluation -Optional. If this option is true , then the value of the computed monitoring property will not be evaluated.
    • disposewhen -Optional. The incoming method. If given, the incoming method will be called after each operation to release the dependency property. The real result is the disposal method that triggers the dependency property.
    • disposewhennodeisremoved -Optional. , passing in the method. If given, the disposal method of the dependency property is triggered when the specified DOM element is deleted by KO. This function is used to release dependency properties when the binding of an element is removed by a template or by a control process binding method.
  3. ko.pureComputed( evaluator [, targetObject] )-Constructs a pure computed observables, using this to invoke evaluator or optional, unlike ko.computed, which does not include the options parameter.

  4. ko.pureComputed( options )-Construct a pure computed observables in another way, she only contains options参数 . The specific parameters can refer to the computed (options) above.

Functions to calculate monitoring properties:

A computed Monitoring property provides the following functions:

    • dispose()-Clears all subscription dependencies.
    • extend(extenders)-Apply the given extension to the calculation monitoring properties.
    • getDependenciesCount()-Returns the current number of dependencies that calculate the monitoring properties.
    • getSubscriptionsCount( [event] )-Returns the number of subscriptions for the user. Alternatively, the "change" count of the newly subscribed is returned through the name of the event (such as).
    • isActive()-Returns whether the dependency property will be updated later, and a dependency property if there is no dependency is not valid.
    • peek()-Returns the value of the current dependency property without creating a dependency (you can refer to the third chapter to calculate the monitoring properties (3) KO How to implement peek in dependency tracking).
    • subscribe( callback [,callbackTarget, event] )-Register a manual subscription to notify of changes to the dependency property.

Determine the type of monitoring property
    • ko.isObservable-Returns True when it is observables, observablearrays, or computed observables.
    • ko.isWritableObservable-Returns True when it is observables, observablearrays, or writable computed observables.
    • ko.isComputed-Returns True when the Monitoring property is calculated.
    • ko.isPureComputed-When it is pure computed observables, return to ture.
Use the context of the calculation monitoring property

During the execution of a computed monitoring property function, you can access ko.computedContext information about the current computed monitoring properties. It provides the following features:

    • isInitial()-Returns True when the computed monitoring property is first run, and returns false at other times. For pure computed observables, isInitial() always undefined .

    • getDependenciesCount()-Returns the current number of dependencies that calculate the monitoring properties.

      • Note: ko.computedContext.getDependenciesCount() the equivalent of calling compute monitoring properties getDependenciesCount() .

Cases:

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 construction methods and functions described above are often useful only in high-level scenarios.

Knockoutjs 3.X API Third Chapter Computing Monitoring properties (5) Reference manual

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: info-contact@alibabacloud.com 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.