Home > Web Front-end > JS Tutorial > body text

Introduction to KnockoutJs

Linda Hamilton
Release: 2024-10-21 22:40:30
Original
906 people have browsed it

Introdução ao KnockoutJs

This content is basically a translation of the original materials. The intention is to learn about KnockoutJs for Magento 2 and create content in Portuguese about KnockouJs.

Documentation

  • Microsoft: MVVM
  • KnockoutJs: Introduction
  • KnockoutJs: Installation

MVVM Pattern

MVVM (Model-View-ViewModel) architecture is a software design pattern widely used in user interface applications. MVVM is particularly useful in applications with complex and interactive user interfaces, as it clearly separates business logic from interface behavior. This facilitates code maintenance, testability and scalability.

When the user interacts with the View, the ViewModel captures these actions and updates the corresponding data in the Model. When the Model is updated, the ViewModel receives these changes and automatically updates the View, reflecting the changes in the user interface. The ViewModel can also perform specific actions, such as sending requests to the server to fetch or send data.

Model: The Model represents the application's data layer. It handles business logic, validation rules, data storage and retrieval. The Model generally consists of data objects that represent entities and functionalities specific to the application domain.

View: The View is the application's presentation layer, responsible for displaying the Model data and interacting with the user. It represents the graphical interface with which the user interacts to visualize and manipulate data. In the MVVM architecture, the View is kept as decoupled as possible from the business logic, making it easy to update and replace.

ViewModel: The ViewModel is the core part of the MVVM pattern. It acts as an intermediary between the View and the Model, performing communication and bidirectional data binding between them. The ViewModel contains the presentation logic, transforming the data from the Model into a form suitable for display in the View. Additionally, the ViewModel manages user events and responses, interacting with the Model as needed.

KnockoutJs

Knockout.js is an open-source JavaScript library that allows you to create dynamic and reactive user interfaces. It is especially useful for developing web applications with complex and interactive user interface. KnockoutJs is based on the MVVM design pattern (Model-View-ViewModel), which separates business logic (Model) from presentation (View) through from an intermediate layer called ViewModel. It is independent of any other structure.

KnockoutJs was developed and is maintained as an open source project by Steve Sanderson, a Microsoft employee on July 5, 2010.

KnockoutJs supports all mainstream browsers - IE 6, Firefox 3.5, Chrome, Opera, Safari (desktop/mobile). The data-bind attribute is not native to HTML, although it is an acceptable attribute (it is strictly compatible with HTML 5 and does not cause problems with HTML 4, although a validator indicates that it is an unrecognized attribute). But since the browser doesn't know what this means, you need to activate KnockoutJs for it to take effect.

When using KnockoutJs, your view layer is simply your HTML document with declarative bindings to link it to the view-model layer. Alternatively, you can use models which generate HTML using data from a model in the view-model.

layer

Simply place the script block at the bottom of an HTML document or place it at the top and wrap the content in a DOM-ready handler like jQuery's $ function.

ko.applyBindings(myViewModel): is a method used to apply data binding in KnockoutJs and bind a ViewModel (or Model) to the interface user (View). It connects the ViewModel to HTML elements in the DOM, allowing data from the ViewModel to be automatically reflected in the UI and vice versa. When calling this method, KnockoutJs traverses the DOM in search of elements that have the KnockoutJs data binding directives and creates the links between these elements and the ViewModel properties specified in the directives .

Optionally, you can pass a second parameter to define which part of the document you want to search for data binding attributes. For example, ko.applyBindings(myViewModel, document.getElementById('someElementId')). This restricts activation to the element with ID someElementId and its descendants, which is useful if you want to have multiple display models and associate each one with a different region of the page.

Installation

Simply reference the JavaScript file using a

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!