Jump to content

Knockout (web framework): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Importing Wikidata short description: "JavaScript library" (Shortdesc helper)
Tapeshm (talk | contribs)
 
(17 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Short description|JavaScript library}}
{{Short description|JavaScript library}}
{{More footnotes|date=June 2021}}
{{Infobox software
{{Infobox software
| name = Knockout
| name = Knockout
| author = [http://blog.stevensanderson.com Steve Sanderson]
| author = Steve Sanderson
| developer =
| developer =
| released = {{start date|2010|07|05}}
| released = {{start date|2010|07|05}}
Line 32: Line 33:
* Declarative bindings
* Declarative bindings
* Automatic UI refresh (when the data model's state changes, the UI updates automatically)
* Automatic UI refresh (when the data model's state changes, the UI updates automatically)
* Dependency tracking Templating (using a native template engine although other templating engines can be used, such as [[jquery.tmpl]])
* Dependency tracking Templating (contains a dedicated template engine, but other templating engines can be used)


== Example ==
== Examples ==


In this example, two text boxes are bound to observable variables on a data model. The "full name" display is bound to a dependent observable, whose value is computed in terms of the observables. When either text box is edited, the "full name" display is automatically updated, with no explicit event handling.
1. In this example, two text boxes are bound to observable variables on a data model. The "full name" display is bound to a dependent observable, whose value is computed in terms of the observables. When either text box is edited, the "full name" display is automatically updated, with no explicit event handling.


=== View Model (JavaScript) ===
=== View Model (JavaScript) ===
Line 51: Line 52:


ko.applyBindings(new ViewModel());
ko.applyBindings(new ViewModel());
</syntaxhighlight>

2. Creating Custom Binding Handlers in KnockoutJS

Use the ko.bindingHandlers object to specify your custom binding’s name and create an init or update function when creating a custom binding handler. The init function is called when the binding has been applied to an element, perfect for onetime initialization. Whenever the bound observable changes, an update function is called that allows you to react to changing data.

Here’s a simple example of a custom binding handler that applies a jQuery UI datepicker to an input element:

=== Custom Binding Handler ===

<syntaxhighlight lang="javascript">
ko.bindingHandlers.datepicker = {
init: function(element, valueAccessor) {
$(element).datepicker({
onSelect: function(date) {
var observable = valueAccessor();
observable(date);
}
});
},
update: function(element, valueAccessor) {
var value = ko.unwrap(valueAccessor());
$(element).datepicker("setDate", value);
}
};
</syntaxhighlight>
</syntaxhighlight>


Line 57: Line 83:
* {{cite news|url=http://msdn.microsoft.com/en-us/magazine/hh781029.aspx|title=Getting Started with Knockout|work=MSDN Magazine|author=Papa, John|date=February 2012|access-date=March 9, 2012}}
* {{cite news|url=http://msdn.microsoft.com/en-us/magazine/hh781029.aspx|title=Getting Started with Knockout|work=MSDN Magazine|author=Papa, John|date=February 2012|access-date=March 9, 2012}}
* {{cite news|url=http://msdn.microsoft.com/en-us/magazine/hh852598.aspx|title=Knockout's Built-in Bindings for HTML and JavaScript|work=MSDN Magazine|author=Papa, John|date=March 2012|access-date=March 9, 2012}}
* {{cite news|url=http://msdn.microsoft.com/en-us/magazine/hh852598.aspx|title=Knockout's Built-in Bindings for HTML and JavaScript|work=MSDN Magazine|author=Papa, John|date=March 2012|access-date=March 9, 2012}}

* {{cite news|url=https://wirefuture.com/post/mastering-knockoutjs-a-comprehensive-guide-for-dynamic-web-apps|title=Mastering Knockoutjs: A Comprehensive Guide for Dynamic Web Apps|date=Feb 2024|access-date=Feb 26, 2024}}


== External links ==
== External links ==
* {{official website|http://knockoutjs.com/}}
* {{official website|http://knockoutjs.com/}}

* [http://knockoutmvc.com/ Knockout MVC: A framework integrating KnockoutJS into ASP.NET]
* [http://channel9.msdn.com/Events/MIX/MIX11/FRM08 Knockout JS: Helping you build dynamic JavaScript UIs with MVVM and ASP.NET]
* [https://web.archive.org/web/20121219181049/http://blog.davidpadbury.com/2011/03/20/using-isotope-with-knockout-js/ Using Isotope with Knockout.js]
* [https://stackoverflow.com/questions/tagged/knockoutjs Knockout questions on StackOverflow]
* [http://groups.google.com/group/knockoutjs Knockout forum on Google Groups]
* [http://www.knockmeout.net/ Knockout JS Examples]
* [https://github.com/onlyurei/knockout-spa Knockout SPA Framework]
* [https://www.endyourif.com/tag/knockoutjs Knockout JS Recipes]


{{JS templating |state=autocollapse}}
{{JS templating |state=autocollapse}}
{{Rich Internet applications}}
{{Rich web applications}}
{{Application frameworks}}
{{Application frameworks}}
{{ECMAScript}}
{{ECMAScript}}


[[Category:Rich Internet application frameworks]]
[[Category:Rich web application frameworks]]
[[Category:Ajax (programming)]]
[[Category:Ajax (programming)]]
[[Category:JavaScript libraries]]
[[Category:JavaScript libraries]]

Latest revision as of 14:29, 1 March 2024

Knockout
Original author(s)Steve Sanderson
Initial releaseJuly 5, 2010 (2010-07-05)
Stable release
3.5.1 / November 5, 2019; 4 years ago (2019-11-05)
RepositoryKnockout Repository
Written inJavaScript
Size59 KB minified / 283 KB (development mode)
TypeJavaScript library
LicenseMIT
Websiteknockoutjs.com

Knockout is a standalone JavaScript implementation of the Model–View–ViewModel pattern with templates. The underlying principles are therefore:

  • a clear separation between domain data, view components and data to be displayed
  • the presence of a clearly defined layer of specialized code to manage the relationships between the view components

The latter leverages the native event management features of the JavaScript language.

These features streamline and simplify the specification of complex relationships between view components, which in turn make the display more responsive and the user experience richer.

Knockout was developed and is maintained as an open source project by Steve Sanderson.

Features

[edit]

Knockout includes the following features:

  • Declarative bindings
  • Automatic UI refresh (when the data model's state changes, the UI updates automatically)
  • Dependency tracking Templating (contains a dedicated template engine, but other templating engines can be used)

Examples

[edit]

1. In this example, two text boxes are bound to observable variables on a data model. The "full name" display is bound to a dependent observable, whose value is computed in terms of the observables. When either text box is edited, the "full name" display is automatically updated, with no explicit event handling.

View Model (JavaScript)

[edit]
function ViewModel() {
    this.firstName = ko.observable("");
    this.lastName = ko.observable("");

    this.fullName = ko.computed(
        function() { return this.firstName() + " " + this.lastName(); }, 
        this);
}

ko.applyBindings(new ViewModel());

2. Creating Custom Binding Handlers in KnockoutJS

Use the ko.bindingHandlers object to specify your custom binding’s name and create an init or update function when creating a custom binding handler. The init function is called when the binding has been applied to an element, perfect for onetime initialization. Whenever the bound observable changes, an update function is called that allows you to react to changing data.

Here’s a simple example of a custom binding handler that applies a jQuery UI datepicker to an input element:

Custom Binding Handler

[edit]
ko.bindingHandlers.datepicker = {
    init: function(element, valueAccessor) {
        $(element).datepicker({
            onSelect: function(date) {
                var observable = valueAccessor();
                observable(date);
            }
        });
    },
    update: function(element, valueAccessor) {
        var value = ko.unwrap(valueAccessor());
        $(element).datepicker("setDate", value);
    }
};

References

[edit]
  • Papa, John (February 2012). "Getting Started with Knockout". MSDN Magazine. Retrieved March 9, 2012.
  • Papa, John (March 2012). "Knockout's Built-in Bindings for HTML and JavaScript". MSDN Magazine. Retrieved March 9, 2012.
[edit]