Top features of knockout

Source: Internet
Author: User
Tags log

One of the best features of knockout is its scalability. Knockout has a large number of extension points that contain a large number of tools to create our applications. Many developers create great sites in addition to knockout core libraries without using any other scripting libraries (even jQuery).

Subscribables

When creating our inventory management program, it is easy to see that observable is a core object in knockout. At the bottom of Observable,observablearray and computed observables is subscribable,subscribable is an object that contains three methods and a subscriptions array. These three methods are:

Subscribe: This method adds a subscription to the subject object, and when the subscribed subject issues a reminder, the subscription is invoked and the default type of reminder is change.

Notifysubscribers: This method invokes all subscriptions and passes a parameter to the callback method of the subscription.

Extend: This method adds an extension to the subject object

The following code demonstrates the use of the first two methods to implement publications and subscriptions.

var test = ko.observable ();  
      
Create a subscription for the "Test-event"  
      
test.subscribe (function (val) {  
      
    console.log (val);  
      
}, Test, " Test-event ");  
      
Test.notifysubscribers ("Hello World", "test-event");

One of the cool features of subscriptions in knockout is that it can be mixed into any Javascript object, and the following code shows how it is used in normal code.

Dummy subscribable  
function PubSub () {  
    //Inherit subscribable ko.subscribable.call  
    (this);  
}  
      
Create a instance of our subscribable  
var pubsub = new PubSub ();  
Make a subscription  
var subscription = Pubsub.subscribe (function (val) {  
    console.log (val);  
}, PubSub, ' Test-topic ');  
      
Pubsub.notifysubscribers ("Hello World", "test-topic");  
Console: "Hello World"  
//clean up things  
subscription.dispose ();

After invoking the Subscribe method of a subscribed object, we get a Subscription object, which usually allows the developer to ignore the subscription object returned by the Subscribe method, but it is necessary to note that this subscription object Subscription We can release the callback method for the subscription and the reference to the callback in the application, and call the Dispose method directly to make sure that your program does not cause memory leaks.

Now that you have understood a core processing mechanism for knockout, subscribable, we will learn how knockout expands its usage in other modes.

Observables

The knockout observable object is the first class object to implement the subscription mechanism, and it is also the simplest and most powerful part of the knockout library. The following code demonstrates the basic idea of implementing observable.

Very Simple Knockout observable implementation//ko.observable is actually a function factory ko.observabl E = function (InitialValue) {//private variable to hold the observable ' s value var _latestvalue = ini  
      
    Tialvalue;  
        The actual "observable" function function observable () {//One or more args, so it ' s a Write  
            if (Arguments.length > 0) {//Set the private variable _latestvalue = arguments[0];  
            Tell the Subscribers that things have changed observable["Notifysubscribers"] (_latestvalue); return this; Permits chained assignments} else {//no args, so it ' s a Read//Just hand B  
        ACK the private variable ' s value return _latestvalue;  
      
    }//Inherit from Subscribable Ko.subscribable.call (observable); Return the freshly created Observable function return observable; };

This is a simple implementation of observable in knockout (the real implementation code contains a lot of robust processing code, validation, dependency detection, and so on), and from this implementation you can see that observable is actually a function of the factory, when the factory method is invoked, It generates a new function. A simple set/get API is implemented here. If you call the returned function without providing a parameter, the _initialvalue is returned, and if a parameter is supplied, the _initialvalue is set and all subscriptions are notified.

When you create an event-driven program, observable can get a lot of applications. We can create event-driven program architectures that rely solely on events (such as button clicks, or changes in input elements, and so on) rather than procedural methods.

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.