-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
856 lines (435 loc) · 26.6 KB
/
index.html
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
<!doctype html>
<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />
<meta name="keywords" content="前端;react" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />
<meta name="description" content="前端 react 笔记">
<meta property="og:type" content="website">
<meta property="og:title" content="静の前端笔记">
<meta property="og:url" content="https://tanyafuzhou.github.io/index.html">
<meta property="og:site_name" content="静の前端笔记">
<meta property="og:description" content="前端 react 笔记">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="静の前端笔记">
<meta name="twitter:description" content="前端 react 笔记">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Mist',
sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
fancybox: true,
motion: true,
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="https://tanyafuzhou.github.io/"/>
<title>静の前端笔记</title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?1dfb0ab55c7addf105d658e489cb7f8f";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<div class="container sidebar-position-left
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">静の前端笔记</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<h1 class="site-subtitle" itemprop="description"></h1>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br />
分类
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="https://tanyafuzhou.github.io/2017/03/12/浅谈大型 react 项目前端构建/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="tanyafuzhou">
<meta itemprop="description" content="">
<meta itemprop="image" content="/logo.png">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="静の前端笔记">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2017/03/12/浅谈大型 react 项目前端构建/" itemprop="url">浅谈大型 react 项目前端构建(持续更新中)</a></h2>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2017-03-12T20:58:28+08:00">
2017-03-12
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/react/" itemprop="url" rel="index">
<span itemprop="name">react</span>
</a>
</span>
</span>
<!--noindex-->
<span class="post-comments-count">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-comment-o"></i>
</span>
<a href="/2017/03/12/浅谈大型 react 项目前端构建/#comments" itemprop="discussionUrl">
<span class="post-comments-count hc-comment-count" data-xid="2017/03/12/浅谈大型 react 项目前端构建/" itemprop="commentsCount"></span>
</a>
</span>
<!--/noindex-->
<span id="/2017/03/12/浅谈大型 react 项目前端构建/" class="leancloud_visitors" data-flag-title="浅谈大型 react 项目前端构建(持续更新中)">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读次数 </span>
<span class="leancloud-visitors-count"></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="一、项目目录"><a href="#一、项目目录" class="headerlink" title="一、项目目录"></a>一、项目目录</h1><img src="/2017/03/12/浅谈大型%20react%20项目前端构建/project.png" alt="project.png" title="">
<p>项目的构建方式大体上是普遍标准的模式,src 是业务开发目录,build 是构建后的目录。</p>
<p>构建工具采用 webpack+express 或 webpack+koa 的方式。</p>
<h1 id="二、src-业务代码的设计"><a href="#二、src-业务代码的设计" class="headerlink" title="二、src 业务代码的设计"></a>二、src 业务代码的设计</h1><img src="/2017/03/12/浅谈大型%20react%20项目前端构建/src.png" alt="src.png" title="">
<h2 id="1-路由"><a href="#1-路由" class="headerlink" title="1.路由"></a>1.路由</h2><p> 这个不用多说,果断用 react-router 进行路由的控制。这里要对路由的构建目录特别说下:</p>
<ul>
<li>App.js 中列出项目中所有的路由。</li>
<li>index.js作为项目的入口文件,只引用 App.js,以及其他 hack。</li>
</ul>
<h2 id="2-公共组件(components)"><a href="#2-公共组件(components)" class="headerlink" title="2.公共组件(components)"></a>2.公共组件(components)</h2><p> 公共的业务组件统一放入 components 目录</p>
<p> <em>注意</em></p>
<p> 公共的含义是:多于一个页面用到的组件称为公共组件,如果只有一个页面用到的组件,不要放到 components 目录,放到对应页面的子目录。</p>
<h2 id="3-高阶函数(hoc"><a href="#3-高阶函数(hoc" class="headerlink" title="3.高阶函数(hoc)"></a>3.高阶函数(hoc)</h2><p> react 中有一个比较特色的功能,就是组件可以传入一个函数中生成新的高阶组件</p>
<p> 常见的应用场景,通常是一些页面经常性的重复操作,可封装成一个高阶函数,进行统一处理,避免重复写代码</p>
<ol>
<li>一些页面一进入就会去加载数据,此时便可使用高阶函数</li>
<li>如一些页面埋点的重复性操作</li>
</ol>
<h2 id="4-样式"><a href="#4-样式" class="headerlink" title="4.样式"></a>4.样式</h2><p> 项目中所有的样式都在 styles 目录下,该目录下的样式尽可能是公共组件的样式</p>
<p> 如果某些页面需要使用样式,直接使用 react 内置的 style功能,这样可以尽可能保持目录的纯净度。</p>
<h2 id="5-redux"><a href="#5-redux" class="headerlink" title="5.redux"></a>5.redux</h2><p> 如果需要使用 redux,可分为 store reducers action 三个目录进行控制</p>
<p> 切记 ,要分清 redux 的应用场景,不要滥用</p>
<h2 id="6-辅助工具(utils)"><a href="#6-辅助工具(utils)" class="headerlink" title="6.辅助工具(utils)"></a>6.辅助工具(utils)</h2><p> 主要存放项目全局需要经常使用到的工具类</p>
<h2 id="7-静态文件(data)"><a href="#7-静态文件(data)" class="headerlink" title="7.静态文件(data)"></a>7.静态文件(data)</h2><p> 主要存放静态常量等</p>
<h1 id="三、pages-目录"><a href="#三、pages-目录" class="headerlink" title="三、pages 目录"></a>三、pages 目录</h1><p>该目录与业务场景强相关,故在此先模拟个业务场景</p>
<h2 id="1-业务背景"><a href="#1-业务背景" class="headerlink" title="1.业务背景"></a>1.业务背景</h2><p>有三个角色 :角色 A、角色 B、角色O(管理员)</p>
<p>有3个业务场景:场景 I、场景 II、场景 III</p>
<p>其中,场景 I 只有角色 A 可以访问,场景 II 只有角色 O 可以访问,场景 III 所有角色均可访问</p>
<h2 id="2-解决方案"><a href="#2-解决方案" class="headerlink" title="2.解决方案"></a>2.解决方案</h2><img src="/2017/03/12/浅谈大型%20react%20项目前端构建/pages.png" alt="pages.png" title="">
<p>角色和场景是一对一关系的时候,可以建以角色为根目录,场景为子目录的目录<br>其中场景 III index.js中,根据角色判断加载哪个角色的目录</p>
<p>角色和场景是多对一的关系的时候,可以建以场景为根目录,角色为子目录的目录。</p>
<h2 id="3-业务组件"><a href="#3-业务组件" class="headerlink" title="3.业务组件"></a>3.业务组件</h2><p>当页面中有该页面独有的业务组件时,请勿将其放入公共 components 组件目录,这样路径太远会不方便查找。</p>
<p>建议将该页面建一个目录,对应的业务组件放在该页面下</p>
<img src="/2017/03/12/浅谈大型%20react%20项目前端构建/A.png" alt="A.png" title="">
<p>场景 I 的部分伪代码如下</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"><span class="comment">//index.js</span></div><div class="line"><span class="keyword">import</span> A <span class="keyword">from</span> <span class="string">'./组件 A'</span></div><div class="line"></div><div class="line"><span class="comment">//组件 A</span></div><div class="line"><span class="keyword">import</span> A<span class="number">.1</span> <span class="keyword">from</span> <span class="string">'./子组件 A.1'</span></div></pre></td></tr></table></figure>
<h3 id="这里有个小技巧"><a href="#这里有个小技巧" class="headerlink" title="这里有个小技巧"></a>这里有个小技巧</h3><p>要充分利用目录下的 index.js文件,它作为目录的入口</p>
<p>比如当你遇到业务升级,增加新功能时,可以按上述方式将页面转成目录。</p>
<img src="/2017/03/12/浅谈大型%20react%20项目前端构建/B.png" alt="B.png" title="">
<p>这样做的好处是:</p>
<p>import 时可以不用加<code>./index.js</code>,不需要改import 的代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">import B from './B'</div></pre></td></tr></table></figure>
<h1 id="四、redux"><a href="#四、redux" class="headerlink" title="四、redux"></a>四、redux</h1><p>不要滥用 redux</p>
<p>项目中当需要全局使用到的变量等的时候,才需要用到 redux。</p>
<p>利用 redux 可以将这个变量注入到你需要注入的页面。</p>
<h1 id="五、代码技巧"><a href="#五、代码技巧" class="headerlink" title="五、代码技巧"></a>五、代码技巧</h1><p>巧用export default</p>
<p>如</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//a.js</span></div><div class="line"><span class="keyword">export</span> <span class="keyword">default</span> xxx</div><div class="line"><span class="keyword">export</span> xxx2</div><div class="line"></div><div class="line"><span class="comment">//引用时</span></div><div class="line"></div><div class="line"><span class="keyword">import</span> xxx,{xxx2} <span class="keyword">from</span> <span class="string">'a'</span></div></pre></td></tr></table></figure>
<h1 id="六、构建工具(webpack)"><a href="#六、构建工具(webpack)" class="headerlink" title="六、构建工具(webpack)"></a>六、构建工具(webpack)</h1><p>使用 webpack 配置时,有些小技巧可以分享</p>
<h2 id="1-alias"><a href="#1-alias" class="headerlink" title="1.alias"></a>1.<code>alias</code></h2><p>开发时,引用其他目录的文件时,经常出现以下代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// index.js</span></div><div class="line"><span class="keyword">import</span> A <span class="keyword">from</span> <span class="string">'../../../a'</span></div></pre></td></tr></table></figure>
<p>当你的目录结构变动时,修改相对路径对你来说,无疑就是一场灾难</p>
<p>其实你可以这么干,指定绝对路径</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">alias: {</div><div class="line"> <span class="attr">_</span>: path.resolve(__dirname, <span class="string">'src'</span>)</div><div class="line">}</div></pre></td></tr></table></figure>
<p>然后你就可以</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// index.js</span></div><div class="line"><span class="keyword">import</span> A <span class="keyword">from</span> <span class="string">'_/a'</span></div></pre></td></tr></table></figure>
<p>当 index.js 在目录中的位置变化时,你就可以不需要修改 index.js的任何代码了</p>
<h2 id="2-external"><a href="#2-external" class="headerlink" title="2.external"></a>2.<code>external</code></h2><p>为了避免react等基础库重复构建,可以利用 webpack的<code>externals</code>参数,避免重复构建。</p>
<p>这样,您需要构建的代码就只有 src 目录下的文件,且文件体积很小。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">externals: { <span class="string">'react'</span>: <span class="string">'React'</span>, <span class="string">'react-dom'</span>: <span class="string">'ReactDOM'</span>, <span class="string">'react-router'</span>: <span class="string">'ReactRouter'</span> }</div></pre></td></tr></table></figure>
<p>但这种方式目前来说有缺陷</p>
<ol>
<li>您必须手动将这些大文件基础库,事先 download 至本地。</li>
<li>node_modules 目录下的 react 库并没什么作用<br>后续可以考虑自动化的方式实现。</li>
</ol>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
</section>
</div>
</div>
<div class="sidebar-toggle">
<div class="sidebar-toggle-line-wrap">
<span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
<span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
</div>
</div>
<aside id="sidebar" class="sidebar">
<div class="sidebar-inner">
<section class="site-overview sidebar-panel sidebar-panel-active">
<div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
<img class="site-author-image" itemprop="image"
src="/logo.png"
alt="tanyafuzhou" />
<p class="site-author-name" itemprop="name">tanyafuzhou</p>
<p class="site-description motion-element" itemprop="description"></p>
</div>
<nav class="site-state motion-element">
<div class="site-state-item site-state-posts">
<a href="/archives">
<span class="site-state-item-count">1</span>
<span class="site-state-item-name">日志</span>
</a>
</div>
<div class="site-state-item site-state-categories">
<span class="site-state-item-count">1</span>
<span class="site-state-item-name">分类</span>
</div>
<div class="site-state-item site-state-tags">
<span class="site-state-item-count">2</span>
<span class="site-state-item-name">标签</span>
</div>
</nav>
<div class="links-of-author motion-element">
</div>
</section>
</div>
</aside>
</div>
</main>
<footer id="footer" class="footer">
<div class="footer-inner">
<div class="copyright" >
©
<span itemprop="copyrightYear">2017</span>
<span class="with-love">
<i class="fa fa-heart"></i>
</span>
<span class="author" itemprop="copyrightHolder">tanyafuzhou</span>
</div>
<div class="powered-by">
由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>
<div class="theme-info">
主题 -
<a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
NexT.Mist
</a>
</div>
</div>
</footer>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
</div>
</div>
<script type="text/javascript">
if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
window.Promise = null;
}
</script>
<script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
<script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
<script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>
<script type="text/javascript">
_hcwp = window._hcwp || [];
_hcwp.push({widget:"Bloggerstream", widget_id: 90480, selector:".hc-comment-count", label: "{\%COUNT%\}" });
(function() {
if("HC_LOAD_INIT" in window)return;
HC_LOAD_INIT = true;
var lang = (navigator.language || navigator.systemLanguage || navigator.userLanguage || "en").substr(0, 2).toLowerCase();
var hcc = document.createElement("script"); hcc.type = "text/javascript"; hcc.async = true;
hcc.src = ("https:" == document.location.protocol ? "https" : "http")+"://w.hypercomments.com/widget/hc/90480/"+lang+"/widget.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hcc, s.nextSibling);
})();
</script>
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<script>AV.initialize("hKIUKO01ConyKVk1C9heBO88-gzGzoHsz", "jPCeDFb56nA4jkHnQgG52fx6");</script>
<script>
function showTime(Counter) {
var query = new AV.Query(Counter);
var entries = [];
var $visitors = $(".leancloud_visitors");
$visitors.each(function () {
entries.push( $(this).attr("id").trim() );
});
query.containedIn('url', entries);
query.find()
.done(function (results) {
var COUNT_CONTAINER_REF = '.leancloud-visitors-count';
if (results.length === 0) {
$visitors.find(COUNT_CONTAINER_REF).text(0);
return;
}
for (var i = 0; i < results.length; i++) {
var item = results[i];
var url = item.get('url');
var time = item.get('time');
var element = document.getElementById(url);
$(element).find(COUNT_CONTAINER_REF).text(time);
}
for(var i = 0; i < entries.length; i++) {
var url = entries[i];
var element = document.getElementById(url);
var countSpan = $(element).find(COUNT_CONTAINER_REF);
if( countSpan.text() == '') {
countSpan.text(0);
}
}
})
.fail(function (object, error) {
console.log("Error: " + error.code + " " + error.message);
});
}
function addCount(Counter) {
var $visitors = $(".leancloud_visitors");
var url = $visitors.attr('id').trim();
var title = $visitors.attr('data-flag-title').trim();
var query = new AV.Query(Counter);
query.equalTo("url", url);
query.find({
success: function(results) {
if (results.length > 0) {
var counter = results[0];
counter.fetchWhenSave(true);
counter.increment("time");
counter.save(null, {
success: function(counter) {
var $element = $(document.getElementById(url));
$element.find('.leancloud-visitors-count').text(counter.get('time'));
},
error: function(counter, error) {
console.log('Failed to save Visitor num, with error message: ' + error.message);
}
});
} else {
var newcounter = new Counter();
/* Set ACL */
var acl = new AV.ACL();
acl.setPublicReadAccess(true);
acl.setPublicWriteAccess(true);
newcounter.setACL(acl);
/* End Set ACL */
newcounter.set("title", title);
newcounter.set("url", url);
newcounter.set("time", 1);
newcounter.save(null, {
success: function(newcounter) {
var $element = $(document.getElementById(url));
$element.find('.leancloud-visitors-count').text(newcounter.get('time'));
},
error: function(newcounter, error) {
console.log('Failed to create');
}
});
}
},
error: function(error) {
console.log('Error:' + error.code + " " + error.message);
}
});
}
$(function() {
var Counter = AV.Object.extend("Counter");
if ($('.leancloud_visitors').length == 1) {
addCount(Counter);
} else if ($('.post-title-link').length > 1) {
showTime(Counter);
}
});
</script>
</body>
</html>