The MVVM framework referred to here refers to a framework similar to Vue, Avalon through a two-way binding, data driven.
Basic principle: Intercept data by Defineproperties Get,set; When the DOM template is loaded, scan the DOM for special named labels, generate the accessor and refresh function for the corresponding tag property, and when the expression of the property is evaluated, Registering the refresh function with the associated expression variable changes the processing array, and completing the data operation automatically triggers a DOM change.
Code implementation on the basis of http://www.cnblogs.com/Aaronjs/rewrite, the code slightly inconsistent,
Code Address: Https://github.com/lukezhang001/mvvmtest
Because it is simple implementation, and reference, the process is not a lot of problems, encountered the problem simple record as follows:
1, the code to write, in the local debugging process found JS loading state has been pending, the problem is that the browser can not see the JS file debugging, and then through the continuous elimination of comparison, found that the code has a dead loop, modified after normal
2, the code of Mvvm.define factory executed two times, the first execution of the returned scope is a normal object, used to generate a description of the properties of the array, after the defineproperties returned after the model to perform a factory. The original is to copy the properties of the Originalmodel to the model, the problem is that for the function, the simple copy context is the original defined context, resulting in the function of the VM object or scope, rather than model, resulting in incorrect property operation.
3, the introduction of Observer.js in the file, the actual is not used, the code inside the function used a lot of this, and foreach will change the function of this, so if the function parameters of the foreach use this, we must pass the final context parameters into
The MVVM framework implements the problem record