


bootstrap is a front-end framework that liberates good things for web developers. the ui it presents is very high-end and classy. in theory, there is no need to write a line of css. just add the appropriate attributes to the tag.
knockoutjs is an mvvm framework implemented in javascript. very cool. for example, after the list data items are added or deleted, there is no need to refresh the entire control fragment or write js to add or delete nodes by yourself. you only need to pre-define the template and attributes that conform to its syntax definition. simply put, we only need to focus on data access.
1. introduction to knockout.js
1. knockout.js and mvvm
nowadays, various front-end frameworks are overwhelming and dazzling. don’t lament that being a programmer is really hard. there are always endless techniques to learn, and when will it be the end, unless you transform! the sea of suffering is boundless, it’s up to you to decide whether you want to turn back or go to the shore!
knockout.js is a lightweight front-end framework based on the mvvm pattern. how lightweight is it? according to the latest version v3.4.0 shown on the official website, it is only 22kb. it can handle the binding of data model and interface dom in a friendly manner. the most important thing is that its binding is two-way. that is to say, if the data model changes, the data on the interface dom will also change accordingly. in turn, the interface dom will change accordingly. if the data on the database changes, the data model will also respond to this change. this can greatly reduce the amount of our front-end code and make our interface easy to maintain. we no longer need to write a lot of event monitoring data models and interface dom changes. the blogger below will illustrate these two points based on a usage example.
knockout.js official website: http://knockoutjs.com
knockout.js open source address: https://github.com/knockout/knockout
mvvm pattern: this is a design pattern for creating user interfaces. mvvm splits it into three parts: model, view, and viewmodel. model is the data model, view is our view, and viewmodel is a view model. used to bind the data model and dom elements on the view. if you have used wpf and silverlight, understanding this shouldn't be a problem; if you haven't, it doesn't matter. after reading this article, you will have a general understanding.
2. the simplest example
generally speaking, if you start using knockout.js from scratch, you need to do at least the following four steps
2.1. go to the official website to download the knockout.js file, and then quote it go to the view page.
<script src="~/scripts/knockout/knockout-3.4.0.min.js"></script>
note: knockout.js does not require the support of jquery. if your project needs to use jquery related operations, please quote jquery; otherwise just reference the above files.
2.2. definition of viewmodel
what is viewmodel? in fact, in js, it looks like a json object. we define a viewmodel:
var myviewmodel = { name: "lilei", profession: "软件工程师", };
2.3, define the label binding data-bind in the view view
<div> 姓名:<label data-bind="text:name"></label><br /> 职业:<input type="text" data-bind="textinput:profession" /> </div>
note: the text corresponding to the input tag needs to use textinput, while the text of the ordinary tag can be text.
2.4. activate binding
after completing the above three steps, you also need to activate the knockout binding
ko.applybindings(myviewmodel);
by doing these four steps, the simplest viewmodel data binding is basically realized.
if you are careful enough, you will find that the ko.applybindings() method has two parameters. the first one is the viewmodel we need to bind. what is the second one? from ko.applybindings(myviewmodel); we can see that the second parameter is an optional parameter, which represents the scope of the label bound to the viewmodel. for example, let's change the above code:
<div> 姓名:<label id="lb_name" data-bind="text:name"></label><br /> 职业:<input type="text" data-bind="textinput:profession" /> </div> ko.applybindings(myviewmodel,document.getelementbyid("lb_name"));
it can be seen that the second parameter limits the scope of myviewmodel, that is to say, it will only take effect if it is bound to the label with id="lb_name". the second parameter is a container tag such as a div, which indicates that the scope of the binding is all sub-tags under the div.
3. monitoring attributes
up to the above four steps , we can't see any effect, all we see is binding the data of a json object to the html tag. what's the point of doing this? doesn't it complicate a simple problem? don't worry, witness the miracle now! as mentioned above, the most important significance of knockout is two-way binding, so how to achieve our two-way binding? the answer is monitoring properties.
in knockout, there are three core monitoring attributes: observables, dependentobservables, and observablearray. the meaning of observe is translated as observation. it doesn’t feel right to call it observation attribute or observation attribute. it’s so appropriate, let’s call it monitoring attribute for now.
3.1. observables: monitoring properties
we change the above example to this:
p>
index3 <script src="~/scripts/knockout/knockout-3.4.0.min.js"></script> <div> 姓名:<label data-bind="text:name"></label><br /> 职业:<input type="text" data-bind="textinput:profession" /> </div>
ko.observable("lilei") the meaning of this sentence is to add the name attribute of the viewmodel as a monitoring attribute. when the name attribute becomes a monitoring attribute, something magical will happen. let's take a look when we write myviewmodel. when:
name changes from the original attribute to a method. that is to say, once ko.observable() is added, the corresponding attribute will become a method. , then the value acquisition and assignment of name need to be processed using myviewmodel.name().
code explanation: obviously myviewmodel.name($(this).val()); this sentence assigns the value of the current text box to the name attribute, because the interface is bound to the name attribute, so the value in the label also changes accordingly. or you may say that this can also be done using the textchange event. as long as the value of the current text box is assigned to the label label, this effect can also be achieved. this is nothing. indeed, your way of writing can achieve the goal, but the significance of our monitoring attributes is that if the value of name is changed anywhere, the interface will change accordingly. instead of assigning values to label tags everywhere, js only needs to pay attention to just click on the method myviewmodel.name(). isn’t it awesome~~
3.2. dependentobservables: monitoring dependency attributes
if you haven’t enjoyed reading the above monitoring attributes? let's take a look at the use of monitoring dependency attributes.
let’s change the code and take a look:
index3 <script src="~/scripts/knockout/knockout-3.4.0.min.js"></script>姓名:
职业:
描述:
code explanation: by adding monitoring dependent attribute ko.dependentobservable() can monitor the value of the des attribute to changes in both name and profession at the same time. if any one of them changes, the label bound to des will trigger the change. the biggest advantage of this is that it avoids the need for our js to go the trouble of operating dom is a bit interesting.
3.3. observablearray; monitoring array
in addition to the above two, ko also supports monitoring of array objects. let’s take a look at an example:
index3 <script src="~/scripts/knockout/knockout-3.4.0.min.js"></script>
code explanation: the above is through ko.observablearray() the method adds monitoring of the array object, that is to say, as long as the array change is made to the deptarr array object anywhere in js, the ui will be triggered to give a corresponding response. one thing to note is that the monitored array is actually the monitored array object itself. changes in the properties of the sub-objects in the array object cannot be monitored. for example, we change the click event to this:
$(function () { $("#btn_test").on("click", function () { deptarr[1].name = "aaa"; }); });
this shows that array monitoring actually monitors the array object itself, and does not monitor attribute changes of elements in the array. if you really need to monitor the attribute changes of the objects in the data, you need to use ko.observable() on the object attributes in the data, and use the two together. those who are interested can try it.
4. common data-bind attributes in ko
above, we used multiple data-bind attributes, so how many such data-bind attributes are there in knockout. here we list some commonly used properties.
4.1, text and inputtext
text, as the name suggests, means text. this binding attribute is generally used for

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor