Repository navigation
Expand file tree
/
Copy pathdata-binding.html
More file actions
968 lines (491 loc) · 42.3 KB
/
Copy pathdata-binding.html
File metadata and controls
968 lines (491 loc) · 42.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>数据监听 · Regular官方指南</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<meta name="author" content="leeluolee">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="filter.html" />
<link rel="prev" href="../reference/expression.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="输入并搜索" />
</div>
<nav role="navigation">
<ul class="summary">
<li>
<a href="https://github.com/regularjs/regular" target="_blank" class="custom-link">Github</a>
</li>
<li class="divider"></li>
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
️介绍
</a>
<ul class="articles">
<li class="chapter " data-level="1.1.1" data-path="../intro/what.html">
<a href="../intro/what.html">
Regular是什么
</a>
</li>
<li class="chapter " data-level="1.1.2" data-path="../intro/hello.html">
<a href="../intro/hello.html">
️你好,Regular
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.2" data-path="./">
<a href="./">
基础
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="component/">
<a href="component/">
️组件
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="interpolation.html">
<a href="interpolation.html">
️插值
</a>
</li>
<li class="chapter " data-level="1.2.3" data-path="event.html">
<a href="event.html">
️事件
</a>
</li>
<li class="chapter " data-level="1.2.4" data-path="statement/">
<a href="statement/">
️语句
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.4.1" data-path="statement/list.html">
<a href="statement/list.html">
️列表渲染 - list
</a>
</li>
<li class="chapter " data-level="1.2.4.2" data-path="statement/if.html">
<a href="statement/if.html">
️条件渲染 - if
</a>
</li>
<li class="chapter " data-level="1.2.4.3" data-path="statement/include.html">
<a href="statement/include.html">
️结构引入 - include
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.2.5" data-path="../reference/expression.html">
<a href="../reference/expression.html">
表达式
</a>
</li>
<li class="chapter active" data-level="1.2.6" data-path="data-binding.html">
<a href="data-binding.html">
数据监听
</a>
</li>
<li class="chapter " data-level="1.2.7" data-path="filter.html">
<a href="filter.html">
计算字段与过滤器
</a>
</li>
<li class="chapter " data-level="1.2.8" data-path="directive.html">
<a href="directive.html">
️指令
</a>
</li>
<li class="chapter " data-level="1.2.9" data-path="animation.html">
<a href="animation.html">
️动画
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="../advanced/">
<a href="../advanced/">
深入组件
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../advanced/modular.html">
<a href="../advanced/modular.html">
继承与模块化
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../advanced/composite.html">
<a href="../advanced/composite.html">
️组合 - 结构复用
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="../advanced/modify.html">
<a href="../advanced/modify.html">
️动态注入
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="../topic/">
<a href="../topic/">
专题
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../topic/template.html">
<a href="../topic/template.html">
模板管理
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1.1" >
<a target="_blank" href="https://github.com/regularjs/regular-loader">
单文件组件 - regular-loader
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.2" >
<a target="_blank" href="https://regularjs.github.io/regular-state/">
️单页系统
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="../topic/ssr.html">
<a href="../topic/ssr.html">
服务端渲染
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="../topic/mpregular.html">
<a href="../topic/mpregular.html">
微信小程序
</a>
</li>
<li class="chapter " data-level="1.4.5" data-path="../topic/performance.html">
<a href="../topic/performance.html">
性能指引
</a>
</li>
<li class="chapter " data-level="1.4.6" data-path="../topic/communication.html">
<a href="../topic/communication.html">
️跨组件通信
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.6.1" data-path="../topic/redux.md">
<span>
与redux结合
</a>
</li>
<li class="chapter " data-level="1.4.6.2" data-path="../topic/rx.md">
<span>
与Rx结合(ing...)
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4.7" data-path="../topic/todomvc.html">
<a href="../topic/todomvc.html">
50行代码的todomvc
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../reference/">
<a href="../reference/">
索引手册
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../reference/api.html">
<a href="../reference/api.html">
API
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="../reference/styleguide.html">
<a href="../reference/styleguide.html">
风格指南
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="../reference/directive.html">
<a href="../reference/directive.html">
✔内建指令
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="../faq/">
<a href="../faq/">
FAQ
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
本书使用 GitBook 发布
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >数据监听</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div class="search-plus" id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<div id="anchor-navigation-ex-navbar"><i class="fa fa-anchor"></i><ul><li><span class="title-icon "></span><a href="#数据监听和计算属性"><b> </b>数据监听和计算属性</a></li><ul><li><span class="title-icon "></span><a href="#基本用法"><b>1. </b>基本用法</a></li><li><span class="title-icon "></span><a href="#deep"><b>2. </b>深度监听 </a></li><li><span class="title-icon "></span><a href="#init"><b>3. </b>直接触发本监听的脏检查 </a></li><li><span class="title-icon "></span><a href="#dirty-check"><b>4. </b>数据监听的秘密 - 脏检查 </a></li><ul><li><span class="title-icon "></span><a href="#what"><b>4.1. </b>什么是脏检查 </a></li><li><span class="title-icon "></span><a href="#digest"><b>4.2. </b>脏检查如何进行 - digest流程 </a></li><li><span class="title-icon "></span><a href="#checksinglewatcher"><b>4.3. </b>checkSingleWatcher</a></li><li><span class="title-icon "></span><a href="#checkwatchers"><b>4.4. </b>checkWatchers</a></li><li><span class="title-icon "></span><a href="#when"><b>4.5. </b>何时进行脏检查 </a></li><li><span class="title-icon "></span><a href="#why"><b>4.6. </b>为什么使用脏检查 </a></li></ul><li><span class="title-icon "></span><a href="#stable"><b>5. </b>监听器的稳定性</a></li></ul></ul></div><a href="#数据监听和计算属性" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="数据监听和计算属性"><a name="数据监听和计算属性" class="anchor-navigation-ex-anchor" href="#数据监听和计算属性"><i class="fa fa-link" aria-hidden="true"></i></a> 数据监听和计算属性</h1>
<p>大部分情况,通过在模板中声明表达式,Regular 会通过 vm 自动完成 view 与 model 的绑定,对于自定义的监听需求,你可以通过 <a href="#watch">数据监听</a>实现</p>
<p>在本文的末尾,也有对<a href="#dirty-check">数据绑定原理:脏检查</a>的介绍。</p>
<p>Regular 内部的所有监听都是通过 <a href="../reference/api.html#watch">$watch API</a>, 你也可以使用这个 API 来实现自定义数据监听</p>
<h2 id="基本用法"><a name="基本用法" class="anchor-navigation-ex-anchor" href="#基本用法"><i class="fa fa-link" aria-hidden="true"></i></a>1. 基本用法</h2>
<pre><code class="lang-js">
<span class="hljs-keyword">new</span> Regular({
template: <span class="hljs-string">`
<intpu r-model={value} />
`</span>,
config( ){
<span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'value'</span>, value=>{
<span class="hljs-keyword">this</span>.data.value = value.slice(<span class="hljs-number">0</span>, <span class="hljs-number">5</span>);
})
}
})
</code></pre>
<script async src="//jsfiddle.net/leeluolee/poryh5sb/embed/result,js/"></script>
<p>这个例子实现了 最多只能输入5个字符 的效果。</p>
<h2 id="deep"><a name="deep" class="anchor-navigation-ex-anchor" href="#deep"><i class="fa fa-link" aria-hidden="true"></i></a>2. 深度监听 </h2>
<p>通过传入<code>deep: true</code> 可以做到对象的深层监听</p>
<pre><code class="lang-js"><span class="hljs-keyword">new</span> Regular({
template:<span class="hljs-string">`
<input r-model={blog.title} />
<input r-model={blog.content} />
{blogStr}
`</span>,
config(data){
data.blog = {title: <span class="hljs-string">''</span>, content: <span class="hljs-string">''</span>};
<span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'blog'</span>, (blog)=>{
<span class="hljs-comment">// blog.title or blog.content is change</span>
data.blogStr = <span class="hljs-built_in">JSON</span>.stringify(blog)
}, {
deep:<span class="hljs-literal">true</span>
})
}
})
</code></pre>
<script async src="//jsfiddle.net/leeluolee/oa8uz6c4/embed/result,js/"></script>
<p>注意 Regular <strong>目前只能监听一级属性</strong>,太复杂的属性建议使用不变数据结构,或 监听序列化后的值的方式 ( 通过<code>JSON.stringify</code>,这种方式仅针对数据不是太复杂的场景 ) </p>
<pre><code class="lang-js">
<span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'JSON.stringify(blog)'</span>, (blogStr)=>{
<span class="hljs-keyword">const</span> blog = <span class="hljs-keyword">this</span>.data.blog; <span class="hljs-comment">// blog is change</span>
})
</code></pre>
<h2 id="init"><a name="init" class="anchor-navigation-ex-anchor" href="#init"><i class="fa fa-link" aria-hidden="true"></i></a>3. 直接触发本监听的脏检查 </h2>
<p>你可以直接通过<code>init:true</code>参数,强制触发当前监听器的脏检查,而不用等待下一轮 <a href="#digest">$digest</a> 流程</p>
<pre><code class="lang-js">
Regular.extend({
init(){
<span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'blog'</span>, ()=>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'同步检查这个watcher'</span>)
},{
init: <span class="hljs-literal">true</span>
})
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'晚于watch回调执行'</span>)
}
})
</code></pre>
<h2 id="dirty-check"><a name="dirty-check" class="anchor-navigation-ex-anchor" href="#dirty-check"><i class="fa fa-link" aria-hidden="true"></i></a>4. 数据监听的秘密 - 脏检查 </h2>
<p>类似Angular ,Regular 的数据绑定基于脏检查实现。</p>
<blockquote>
<p>脏检查即一种不关心你如何以及何时改变的数据,只关心在<strong>特定的检查阶段</strong>数据是否改变的数据监听技术。</p>
</blockquote>
<p>本节包含内容</p>
<ul>
<li><a href="#what">什么是脏检查</a></li>
<li><a href="#digest">脏检查是如何进行的</a></li>
<li><a href="#when">何时进行脏检查</a></li>
<li><a href="#why">为什么选脏检查</a></li>
<li><a href="#stable">监听器的稳定性</a></li>
</ul>
<blockquote>
<p>统一使用 Angular 生态圈的术语,方便开发者理解</p>
</blockquote>
<h3 id="what"><a name="what" class="anchor-navigation-ex-anchor" href="#what"><i class="fa fa-link" aria-hidden="true"></i></a>4.1. 什么是脏检查 </h3>
<p>以<a href="interpolation.html#text">文本插值</a> <code>{post.title}</code>为例,在compile阶段,Regular 内部的处理逻辑如下。</p>
<pre><code class="lang-javascript">
walkers.expression = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"> expression </span>)</span>{
<span class="hljs-keyword">var</span> node = <span class="hljs-built_in">document</span>.createTextNode(<span class="hljs-string">""</span>);
<span class="hljs-keyword">this</span>.$watch(expression, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">newval</span>)</span>{
dom.text(node, <span class="hljs-string">""</span> + (newval == <span class="hljs-literal">null</span>? <span class="hljs-string">""</span>: <span class="hljs-string">""</span> + newval) );
})
<span class="hljs-keyword">return</span> node;
}
</code></pre>
<p>这段代码很好理解: 遇到文本插值时,Regular 会创建一个 textNode,每当数据变化时,修改 textNode 的 textContent 值。</p>
<p><strong>说明</strong></p>
<ul>
<li>expression: 表达式: 即<code>post.title</code>,反映在组件就是component.data.post.title.</li>
<li><a href="../reference/api.html#watch">$watch</a>: 添加一个数据监听器(watcher对象)</li>
</ul>
<p><strong>那么问题来了,怎么判断值发生改变了?</strong></p>
<h3 id="digest"><a name="digest" class="anchor-navigation-ex-anchor" href="#digest"><i class="fa fa-link" aria-hidden="true"></i></a>4.2. 脏检查如何进行 - digest流程 </h3>
<p>首先,上例通过<code>$watch</code>接口产生的watcher对象看起来是这样的</p>
<pre><code class="lang-js">
{
get: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">context</span>)</span>{...} <span class="hljs-comment">//获得表达式当前求值,此函数在解析时,已经生成</span>
set: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{} <span class="hljs-comment">// 有些表达式可以生成set函数,用于处理赋值,这个一般用于双向绑定的场景</span>
once: <span class="hljs-literal">false</span> <span class="hljs-comment">// 此监听器是否只生效一次,如 {@(expression)}</span>
last: <span class="hljs-literal">undefined</span><span class="hljs-comment">// 上一次表达式的求值结果</span>
fn: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">newvalue, oldvalue</span>)</span>{} <span class="hljs-comment">// 即你传入$watch的第二个参数,当值改变时,会调用此函数</span>
<span class="hljs-comment">// ...</span>
}
</code></pre>
<p>这是必须引出内部的重要阶段 - <strong>digest阶段</strong> ,当系统进入此阶段时,将会进行数据检查,它的处理流程如下: </p>
<ol>
<li>标记 <code>dirty = false</code></li>
<li>遍历数据观察者watcher,一旦有<a href="#checkSingle">监听器值发生改变</a>,就标记<code>dirty=true</code></li>
<li><a href="#checkAll">检查一轮后</a>,如果 <code>dirty===true</code> ,我们重新进入步骤1. 否则进入步骤4.</li>
<li>完成脏检查</li>
</ol>
<h3 id="checksinglewatcher"><a name="checksinglewatcher" class="anchor-navigation-ex-anchor" href="#checksinglewatcher"><i class="fa fa-link" aria-hidden="true"></i></a>4.3. checkSingleWatcher</h3>
<p>先来看下简化后的检查单个监听器逻辑 <code>checkSingleWatcher</code></p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkSingleWatcher</span>(<span class="hljs-params"> watcher </span>)</span>{
<span class="hljs-keyword">let</span> newValue = watcher.get( <span class="hljs-keyword">this</span> )
<span class="hljs-keyword">if</span>( newValue !== watcher.last ) {
watcher.fn( newValue, watcher.last )
watcher.last = newValue
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
}
</code></pre>
<h3 id="checkwatchers"><a name="checkwatchers" class="anchor-navigation-ex-anchor" href="#checkwatchers"><i class="fa fa-link" aria-hidden="true"></i></a>4.4. checkWatchers</h3>
<p>下例是简化后的检查所有监听器的逻辑</p>
<pre><code class="lang-js">
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkWatchers</span>(<span class="hljs-params"> isStable </span>)</span>{
<span class="hljs-keyword">var</span> watchers = isStable?
<span class="hljs-keyword">this</span>._stableWatchers : <span class="hljs-keyword">this</span>._watchers;
<span class="hljs-keyword">var</span> children = <span class="hljs-keyword">this</span>._children;
<span class="hljs-keyword">var</span> dirty = <span class="hljs-literal">false</span>;
<span class="hljs-comment">// 只要有一个监听器有变化,则认为dirty</span>
watchers.forEach( watcher=>{
<span class="hljs-keyword">if</span>( checkSingleWatcher(watcher) )
dirty = <span class="hljs-literal">true</span>
})
<span class="hljs-comment">//递归遍历所有子组件的监听器 </span>
children.forEach( comp=> {
<span class="hljs-keyword">if</span>(comp.checkWatchers(isStable))
dirty = <span class="hljs-literal">true</span>
})
<span class="hljs-keyword">return</span> dirty;
}
</code></pre>
<p>好,现在我们了解数据检查的内部流程了,但是<strong>何时进入digest阶段</strong></p>
<h3 id="when"><a name="when" class="anchor-navigation-ex-anchor" href="#when"><i class="fa fa-link" aria-hidden="true"></i></a>4.5. 何时进行脏检查 </h3>
<p>由于脏检查没有任何数据劫持逻辑(比如Vue),数据模型本身是无状态的,所以是无法得知数据的变更时机的,可以猜测 digest 阶段必然是 <strong>主动进入的</strong> 。</p>
<p>在 Regular 中,digest 阶段是由<a href="../reference/api.html#update"><code>$update</code></a>方法触发的。</p>
<p><strong>Example</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> component = <span class="hljs-keyword">new</span> Regular();
component.data.name = <span class="hljs-string">'leeluolee'</span>
<span class="hljs-comment">// you need call $update to Synchronize data and view </span>
component.$update();
</code></pre>
<p>值得庆幸的是,大部分情况下都会自动进入 digest 阶段。比如事件、timeout 模块等等。</p>
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-name">button</span> <span class="hljs-attr">on-click</span>=<span class="hljs-string">{blog.title='Hello'}</span>></span>{blog.title}<span class="hljs-tag"></<span class="hljs-name">button</span>></span>
</code></pre>
<script async src="//jsfiddle.net/leeluolee/19mzaxe2/2/embed/result,js/"></script>
<h3 id="why"><a name="why" class="anchor-navigation-ex-anchor" href="#why"><i class="fa fa-link" aria-hidden="true"></i></a>4.6. 为什么使用脏检查 </h3>
<ol>
<li>脏检查完全不关心你改变数据的方式,而常规的set, get的方式则会强加许多限制</li>
<li>脏检查可以实现批处理完数据之后,再去统一更新view.</li>
<li>脏检查其实比 GET/SET 更容易实现。脏检查是个单向的检查流程(请不要和双向绑定发生混淆),可以实现<em>_任意复杂度的表达式支持</em>。而get/set的方式则需要处理复杂依赖链,基本上表达式支持都是阉割的(使用时就像踩雷).</li>
</ol>
<p>但脏检查有时是低效的,因为每次检查都会无脑的遍历(Regular已经对Loop做了优化)。</p>
<p>但与MVVM结合时,他又是高效的。因为数据监听模式带来了DOM的局部更新(而且是准确更新,而不是Virtual DOM那样需要做 <code>diff-patch</code> 操作),而DOM操作恰恰又是隐藏的性能瓶颈所在。</p>
<h2 id="stable"><a name="stable" class="anchor-navigation-ex-anchor" href="#stable"><i class="fa fa-link" aria-hidden="true"></i></a>5. 监听器的稳定性</h2>
<p>Regular 的数据监听器分为两类</p>
<ul>
<li><strong>不稳定的监听器</strong>: 即监听器回调运行可能导致别的数据变动,此时我们应该再进行<code>$digest</code>流程,才能判断值是否真的稳定<pre><code class="lang-js"><span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'title'</span>, (title)=>{
<span class="hljs-keyword">this</span>.data.summary = title.slice(<span class="hljs-number">0</span>, <span class="hljs-number">10</span>);
})
</code></pre>
这个监听器触发变更时,会导致<code>summary</code>的字段的变更。</li>
</ul>
<ul>
<li><strong>稳定的监听器</strong>: 即回调运行不会导致别的数据变动,比如大部分内部实现的指令和插值等监听器<pre><code class="lang-js"><span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'title'</span>, (title)=>{
<span class="hljs-keyword">this</span>.$ref.container.setAttribute(<span class="hljs-string">'data-title'</span>, title);
})
</code></pre>
这个监听器触发变更时,只是修改了DOM,但并不影响别的字段</li>
</ul>
<p>实际Regular的脏检查流程是</p>
<ul>
<li>检查不稳定监听器直到稳定</li>
<li>检查 <strong>一次</strong> 稳定的监听器</li>
</ul>
<p>你也可以通过传入<code>stable:true</code>来标记这个监听器是稳定的,来得到更好的性能表现</p>
<p><strong>Example</strong></p>
<pre><code class="lang-js">
<span class="hljs-keyword">this</span>.$watch(<span class="hljs-string">'title'</span>, (title)=>{
<span class="hljs-keyword">this</span>.$ref.container.setAttribute(<span class="hljs-string">'data-title'</span>, title );
}, {stable: <span class="hljs-literal">true</span>})
</code></pre>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="../reference/expression.html" class="navigation navigation-prev " aria-label="Previous page: 表达式">
<i class="fa fa-angle-left"></i>
</a>
<a href="filter.html" class="navigation navigation-next " aria-label="Next page: 计算字段与过滤器">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"数据监听","level":"1.2.6","depth":2,"next":{"title":"计算字段与过滤器","level":"1.2.7","depth":2,"path":"basic/filter.md","ref":"basic/filter.md","articles":[]},"previous":{"title":"表达式","level":"1.2.5","depth":2,"path":"reference/expression.md","ref":"reference/expression.md","articles":[]},"dir":"ltr"},"config":{"plugins":["edit-link","splitter","-lunr","-search","-sharing","search-plus","expandable-chapters-small","anchor-navigation-ex","force-http"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"splitter":{},"force-http":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":false,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"expandable-chapters-small":{},"edit-link":{"label":"编辑当前页","base":"https://github.com/regularjs/guide/edit/master/"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"search-plus":{}},"theme":"default","author":"leeluolee","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Regular官方指南","language":"zh-hans","links":{"sidebar":{"Github":"https://github.com/regularjs/regular"}},"gitbook":"3.2.3","description":"Regular官方指南"},"file":{"path":"basic/data-binding.md","mtime":"2018-07-26T02:32:27.152Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-08-23T07:40:58.484Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
<script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
<script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
<script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
<script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
<script src="../gitbook/gitbook-plugin-force-http/test.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>