Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Tiny Vue</title>
  6. </head>
  7. <body>
  8.  
  9. <div id="demo">
  10. <p v-html="message"></p>
  11. <input type="text" v-model="message" />
  12. </div>
  13.  
  14. <script id="jsbin-javascript">
  15. // tiny vue
  16. var Vue = function(option){
  17. this.el = option.el
  18. this.data = option.data
  19. this._init()
  20. }
  21.  
  22. Vue.prototype = {
  23. _init: function(){
  24. var prop, Directive, element, dirName, i, l
  25.  
  26. // 把 data 变成属性
  27. for(prop in this.data) {
  28. this._prop(prop)
  29. }
  30.  
  31. // 遍历模板,解析Directive
  32. var children = this.el.children,
  33. directives = {
  34. 'v-model': ModelDirective,
  35. 'v-html': HtmlDirective
  36. }
  37.  
  38. for(i=0,l=children.length; i<l; i++) {
  39. element = children[i]
  40.  
  41. for(dirName in directives){
  42. prop = element.getAttribute(dirName)
  43. Directive = directives[dirName]
  44.  
  45. this._addObserver(
  46. prop,
  47. new Directive(element, this, prop)
  48. )
  49. }
  50. }
  51. },
  52.  
  53. // 属性观察者
  54. _observers: {},
  55. _addObserver: function(prop, observer) {
  56. if(this._observers[prop])
  57. this._observers[prop].push(observer)
  58. else
  59. this._observers[prop] = [observer]
  60. },
  61.  
  62. // 声明属性
  63. _prop: function(prop) {
  64. Object.defineProperty(this, prop, {
  65. get: function(){
  66. return this.data[prop]
  67. },
  68. set: function(val){
  69. var oldVal = this.data[prop]
  70. this.data[prop] = val
  71. this._observers[prop].forEach(function(observer){
  72. observer.updateDom(val, oldVal)
  73. })
  74. }
  75. })
  76. }
  77.  
  78. }
  79.  
  80. function Directive(el, vm, prop) {
  81. this.el = el
  82. this.vm = vm
  83. this.prop = prop
  84.  
  85. this.updateDom(vm[this.prop])
  86.  
  87. if(this.bind) {
  88. this.bind()
  89. }
  90. }
  91.  
  92. Directive.prototype = {
  93. updateDom: null,
  94. updateData: null
  95. }
  96.  
  97. Directive.extend = function(proto) {
  98. var SubClass = function() {
  99. Directive.apply(this, Array.prototype.slice.call(arguments))
  100. }
  101. SubClass.prototype = proto
  102. return SubClass
  103. }
  104.  
  105.  
  106. // Model directive
  107. var ModelDirective = Directive.extend({
  108. bind: function(){
  109. this.el.onkeyup = this.updateData.bind(this)
  110. },
  111. updateDom: function(val){
  112. this.el.value = val
  113. },
  114. updateData: function(event){
  115. this.vm[this.prop] = event.target.value
  116. }
  117. })
  118.  
  119. // Html Directive
  120. var HtmlDirective = Directive.extend({
  121. updateDom: function(val) {
  122. this.el.innerHTML = val
  123. }
  124. })
  125.  
  126.  
  127. // init
  128. new Vue({
  129. el: document.getElementById('demo'),
  130. data: {
  131. message: 'Hello, Vue!'
  132. }
  133. })
  134. </script>
  135.  
  136.  
  137.  
  138. <script id="jsbin-source-javascript" type="text/javascript">// tiny vue
  139. var Vue = function(option){
  140. this.el = option.el
  141. this.data = option.data
  142. this._init()
  143. }
  144.  
  145. Vue.prototype = {
  146. _init: function(){
  147. var prop, Directive, element, dirName, i, l
  148.  
  149. // 把 data 变成属性
  150. for(prop in this.data) {
  151. this._prop(prop)
  152. }
  153.  
  154. // 遍历模板,解析Directive
  155. var children = this.el.children,
  156. directives = {
  157. 'v-model': ModelDirective,
  158. 'v-html': HtmlDirective
  159. }
  160.  
  161. for(i=0,l=children.length; i<l; i++) {
  162. element = children[i]
  163.  
  164. for(dirName in directives){
  165. prop = element.getAttribute(dirName)
  166. Directive = directives[dirName]
  167.  
  168. this._addObserver(
  169. prop,
  170. new Directive(element, this, prop)
  171. )
  172. }
  173. }
  174. },
  175.  
  176. // 属性观察者
  177. _observers: {},
  178. _addObserver: function(prop, observer) {
  179. if(this._observers[prop])
  180. this._observers[prop].push(observer)
  181. else
  182. this._observers[prop] = [observer]
  183. },
  184.  
  185. // 声明属性
  186. _prop: function(prop) {
  187. Object.defineProperty(this, prop, {
  188. get: function(){
  189. return this.data[prop]
  190. },
  191. set: function(val){
  192. var oldVal = this.data[prop]
  193. this.data[prop] = val
  194. this._observers[prop].forEach(function(observer){
  195. observer.updateDom(val, oldVal)
  196. })
  197. }
  198. })
  199. }
  200.  
  201. }
  202.  
  203. function Directive(el, vm, prop) {
  204. this.el = el
  205. this.vm = vm
  206. this.prop = prop
  207.  
  208. this.updateDom(vm[this.prop])
  209.  
  210. if(this.bind) {
  211. this.bind()
  212. }
  213. }
  214.  
  215. Directive.prototype = {
  216. updateDom: null,
  217. updateData: null
  218. }
  219.  
  220. Directive.extend = function(proto) {
  221. var SubClass = function() {
  222. Directive.apply(this, Array.prototype.slice.call(arguments))
  223. }
  224. SubClass.prototype = proto
  225. return SubClass
  226. }
  227.  
  228.  
  229. // Model directive
  230. var ModelDirective = Directive.extend({
  231. bind: function(){
  232. this.el.onkeyup = this.updateData.bind(this)
  233. },
  234. updateDom: function(val){
  235. this.el.value = val
  236. },
  237. updateData: function(event){
  238. this.vm[this.prop] = event.target.value
  239. }
  240. })
  241.  
  242. // Html Directive
  243. var HtmlDirective = Directive.extend({
  244. updateDom: function(val) {
  245. this.el.innerHTML = val
  246. }
  247. })
  248.  
  249.  
  250. // init
  251. new Vue({
  252. el: document.getElementById('demo'),
  253. data: {
  254. message: 'Hello, Vue!'
  255. }
  256. })</script></body>
  257. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement