Jump to content

Knockout (web framework): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Tapeshm (talk | contribs)
 
(42 intermediate revisions by 30 users not shown)
Line 1: Line 1:
{{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}}
| latest release version = 3.4.2
| latest release version = 3.5.1
| latest release date = {{release date|2017|03|06}}
| latest release date = {{release date and age|2019|11|05}}
| repo = {{URL|https://github.com/knockout/knockout|Knockout Repository}}
| status = Active
| programming language = [[JavaScript]]
| programming language = [[JavaScript]]
| size = 59 KB minified / 283 KB (development mode)
| size = 59 KB minified / 283 KB (development mode)
| genre = [[JavaScript library]]
| genre = [[JavaScript library]]
| license = [[MIT License|MIT]]
| license = [[MIT License|MIT]]
| website = {{url|http://knockoutjs.com/}}
| website = {{url|http://knockoutjs.com/}}
}}
}}
{{Portal|Free software}}
{{Portal|Free and open-source software}}
'''Knockout''' is a standalone [[JavaScript]] implementation of the [[Model-View-ViewModel]] pattern with templates. The underlying principles are therefore:
'''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
* a clear separation between domain data, view components and data to be displayed
Line 23: Line 25:
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.
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 software|open source]] project by Steve Sanderson.
Knockout was developed and is maintained as an [[open source]] project by Steve Sanderson, a [[Microsoft]] employee. As the author said, "it continues exactly as-is, and will evolve in whatever direction I and its user community wishes to take it", and stressed, "this isn’t a Microsoft product".<ref name="Hello, Microsoft">{{cite web | url = http://blog.stevensanderson.com/2010/11/03/hello-microsoft/ | title = Steven Sanderson's blog post 'Hello, Microsoft' | date = November 3, 2010 | publisher = blog.stevensanderson.com | accessdate = 2014-10-22 }}</ref>


== Features ==
== Features ==
Line 31: 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 (contains a dedicated template engine, but other templating engines can be used)
* Dependency tracking
* Templating (using a native template engine although other templating engines can be used, such as [[jquery.tmpl]])


== Simple 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 (HTML) ===

<source lang="xml">
<p>First name: <input data-bind="value: testname" /></p>
<p>Last name: <input data-bind="value: testname" /></p>
<h2>Hello, <span data-bind="text: fullName"></span>!</h2>
</source>


=== View Model (JavaScript) ===
=== View Model (JavaScript) ===


<source lang="javascript">
<syntaxhighlight lang="javascript">
function ViewModel() {
function ViewModel() {
this.firstname = ko.observable("");
this.firstName = ko.observable("");
this.lastName = ko.observable("");
this.lastName = ko.observable("");


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


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

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>


== References ==
== References ==
{{Reflist}}
{{Reflist}}
* {{cite web|url=http://blog.stevensanderson.com/2010/11/03/hello-microsoft/|title=Hello, Microsoft|work=Steven Sanderson's blog|author=Sanderson, Steven|date=3 November 2010|accessdate=12 August 2014}}
* {{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|accessdate=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|accessdate=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]
* [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]


{{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]