Detailed Vue data binding binding principle

Since Angular Fire, various MVC frames sprayed, and angular is comparative, but his pit is still quite, and many performance issues are spit. For example, the dirty inspection mechanism of the potholes, the data binding is a favorite, dirty examination is a bit … performance is low. Sometimes it has changed a place, the dirty cycle is multiple times to ensure that the data is really changed and whether it stops changing. Such performance is very low. So people began to study new two-way data binding methods. Emperaty Vue Binding is an implementation of this person. This article follows an example of especially an example to explain the principle of VUE’s data binding.

Data Binding, typically, the regular matching template, then the code is switched to the programmer given the DATA. Bidirectional binding In addition to the dirty inspection mechanism, the two-way data binding implemented by ES5 DEFINEPROPERTY, intercepts the SET and GET methods of the object, which is more effective. The same Avalon is also this method, saying in the right way: “I just intercepted ‘='” when Var Data = 1. The principle is the same. We directly appear: (here followed by especially footsteps) (replication)

      Ideal     {MSG}} 

{{msg}}

{msg}}

{{what} }

{{hey}}

VAR Bindingmark = 'Data-Element-Binding' Function Element (ID, INITDATA) {Var self = this, el = self.el = document.getlementById (ID) Bindings = {} // internal temporary binding data and DOM DATA = Self.Data = {} // Store Bingding Data and Monitor Content = EL .innerhtml.replace (/ {{}/ g, marktoken) el.innerhtml = content for (var "; //) {Bind (variable); // 'MSG' Bind to Data} if (InitData) {Data [variable] = initdata [variable]}} {bindings [variable] = {} // bindings The fields stored in data sources such as bindings ['msg'] return '
'} function bind (variable) {bindings [variable] .ls = el.queryselectorall ('[' + bindingmark + '= " '+ varIABLE + '"]') // Bindings Repalequate Elements of MSG bindings; []. foreach.call (bindings [variable] .ls, function (e) {// Remove Data-Element-Binding property E.RemoveAttribute (bindingmark)}) Object.defineProperty (Data, Variable, {// ES5 observation attribute set: function (newval) {[] .foreach.call (bindings [variable] .ls, function (e) {bindings [variable]. Value = e.textcontent = newval // => Here is the implementation of the binding, update data to the DOM and update internal temporary data})}, get: function () {return bindings [variable] .value // Take data Just internal temporary data}}}} var app = new element ('test', {msg: 'Hello', what: 'hi'}
This should be the principle of Vue data binding. Some places are written in the comment..
What is the process of this data binding?

Start getting a property placeholder Data-element-directing, and the content of the original element is added to add this property. The value of the attribute is the key of the bound value, such as MSG. It is hello, then Data-Element-Binding = “MSG”, while saving this key in the Bindings object while matching, bindings temporarily stores the binding data. Traversing a Bindings object, and then exist of each of the Data-Element-Binding elements in its key (such as MSG) to bindings, then delete the Data-Element-Binding property in the DOM.

The most important thing is:

It maintains a Data object, this Data object is the key to binding, and he is the interface of M and V. He loops to define the property key (such as MSG), then update the DOM data when SET, the completed binding is hijacked “=” operation, and save this value to bindings. The time returned when SET is the temporary data in Bindings. This kind of benefit is that I always use JS’s native way, I change Data will achieve two-way bindings. v => The binding can only need an onchange event to avoid a loop check.

Vue’s single binding is the case, interesting place is that you modify DATA at the console, the content of the P tag will change accordingly, this is the characteristics of the ES5 SET method, such two-way tie It is good to maintain and there is no side effects, and performance is still very powerful.

After seeing the data binding of Vue, we implemented a two-way data binding of an input (saying that the source code is not the same as the source code)

In fact, the two-way binding of the input is very simple, because VUE has helped We do Data properties, then I have already completed M to V when I operate DATA.Bidirectional binding. Then the principle is very simple, and the Data data is modified when onkey is ONKEY. Pioneer code: (copy available)

Ideal
     VAR Bindingmark = 'Data-Element-Binding' Function Element (Classa, InitData) {var Self.l = Document.GtelementsByClassName (Classa), // Multiple Input Change to Class bindings = {} data = self.data = {} for (VAR i = 0; i  textcontent changed to INPUT Value})}, get: function () {return bindings [variable] .value}}) }} VAR App = New Element ('Test', {MSG: 'Hello', Hey: 'AAA'} Function Handlechange (E) {// Binds of V => M E = E|| window.event var key = e.target.outerHtml.match (/ data-element-binding = \ "(. *) \" /) [1]; data [key] = E.target.Value console.log (Data.hey, Data.msg);}      The modified place is also commented, and the result will see when I open. Atput has been binding MSG and Hey:     When I have input:    
When operating data:

详解vue的数据binding绑定原理 Bidirectional binding is OK, what?

The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds.

详解vue的数据binding绑定原理

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment