Knockout (web framework): Difference between revisions
added release age; deleted defunct 'status |
Add web frameworks category |
||
(39 intermediate revisions by 28 users not shown) | |||
Line 1: | Line 1: | ||
{{Short description|JavaScript library}} |
|||
{{More footnotes|date=June 2021}} |
|||
{{Infobox software |
{{Infobox software |
||
| name |
| name = Knockout |
||
| author |
| author = Steve Sanderson |
||
| developer |
| developer = |
||
| released |
| released = {{start date|2010|07|05}} |
||
| latest release version = 3. |
| latest release version = 3.5.1 |
||
| latest release date |
| latest release date = {{release date and age|2019|11|05}} |
||
⚫ | |||
| status = Active |
|||
| programming language |
| programming language = [[JavaScript]] |
||
| size |
| size = 59 KB minified / 283 KB (development mode) |
||
| genre |
| genre = [[JavaScript library]] |
||
| license |
| license = [[MIT License|MIT]] |
||
| website |
| website = {{url|http://knockoutjs.com/}} |
||
}} |
}} |
||
{{Portal|Free software}} |
{{Portal|Free and open-source software}} |
||
'''Knockout''' is a standalone [[JavaScript]] implementation of the [[ |
'''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 |
|||
⚫ | |||
== |
== 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: firstName" /></p> |
|||
<p>Last name: <input data-bind="value: lastName" /></p> |
|||
<h2>Hello, <span data-bind="text: fullName"></span>!</h2> |
|||
</source> |
|||
=== View Model (JavaScript) === |
=== View Model (JavaScript) === |
||
< |
<syntaxhighlight lang="javascript"> |
||
function ViewModel() { |
function ViewModel() { |
||
this.firstName = ko.observable(""); |
this.firstName = ko.observable(""); |
||
Line 59: | Line 52: | ||
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 |
* {{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/ |
* {{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] |
|||
⚫ | |||
{{JS templating |state=autocollapse}} |
{{JS templating |state=autocollapse}} |
||
{{Rich |
{{Rich web applications}} |
||
{{Application frameworks}} |
{{Application frameworks}} |
||
{{ECMAScript}} |
{{ECMAScript}} |
||
[[Category:Rich |
[[Category:Rich web application frameworks]] |
||
[[Category:Ajax (programming)]] |
[[Category:Ajax (programming)]] |
||
[[Category:JavaScript libraries]] |
[[Category:JavaScript libraries]] |
||
[[Category:JavaScript web frameworks]] |
|||
[[Category:Web frameworks]] |
Latest revision as of 15:12, 20 September 2024
This article includes a list of general references, but it lacks sufficient corresponding inline citations. (June 2021) |
Original author(s) | Steve Sanderson |
---|---|
Initial release | July 5, 2010 |
Stable release | 3.5.1
/ November 5, 2019 |
Repository | Knockout Repository |
Written in | JavaScript |
Size | 59 KB minified / 283 KB (development mode) |
Type | JavaScript library |
License | MIT |
Website | knockoutjs |
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.
- "Mastering Knockoutjs: A Comprehensive Guide for Dynamic Web Apps". Feb 2024. Retrieved Feb 26, 2024.
External links
[edit]