|
11 | 11 | // by other framework I tried before. I feel today that Redux is a much better way to learn about flux
|
12 | 12 | // than many other framework. That's why I want now to share with everyone, using my own words,
|
13 | 13 | // flux concepts that I am starting to grasp, focusing on the use of Redux.
|
| 14 | +// |
| 15 | +// 为什么开始这个教程? |
| 16 | +// 在学习redux的过程中,我意识到,之前通过阅读文章及个人经验积累的关于FLUX的认识是不正确的。 |
| 17 | +// 我并不是说关于FLUX的介绍文章写得不好,而是我不正确把握的概念。 |
| 18 | +// 最后,我得到的只是运用不同的FLUX框架(Reflux,Flummox,FB-FLUX),并试图让他们的匹配我所学到的理论概念, |
| 19 | +// (actions、actions creators,store,dispatcher等)。 |
| 20 | +// 当我开始使用Redux时,我才意识到,FLUX比我想象的更简单。这一切都归功于Redux被设计得非常好,并已经删除了很多其他框架中 |
| 21 | +// 我尝试引进过的“反样板特性”。 |
| 22 | +// 现在,比许多其他的框架,我觉得通过Redux去了解FLUX是一个更好的方式。 |
| 23 | +// 这就是为什么我要用自己的理解与大家一起分享FLUX概念。我开始尝试重点使用Redxu。 |
| 24 | + |
| 25 | + |
14 | 26 |
|
15 | 27 | // You may have seen this diagram representing the famous unidirectional data flow of a flux application:
|
| 28 | +// |
| 29 | +// 您可能已经看到过这个表示Flux的应用单向数据流的著名图表: |
16 | 30 |
|
17 | 31 | /*
|
18 | 32 | _________ ____________ ___________
|
|
39 | 53 | // to explain this complete diagram and the overall flow it describes, we'll take each piece separately and try to
|
40 | 54 | // understand why it exists and what role it plays. In the end you'll see that this diagram makes perfect sense
|
41 | 55 | // once we understood each of its parts.
|
| 56 | +// |
| 57 | +// 在本教程中,我们将逐步向您介绍上图的概念。我们不试图完整的解释图表中数据流的描述,而是 |
| 58 | +// 分别介绍每个部件并试图了解它为什么存在,它发挥什么样的作用。 |
| 59 | +// 最后,一旦我们了解它的每一个部分,你会看到,这个图是非常合情合理的。 |
| 60 | +// |
42 | 61 |
|
43 | 62 | // But before we start, let's speak a little bit about why flux exists and why we need it...
|
44 | 63 | // Let's pretend we're building a web application. What are all web applications made of?
|
45 | 64 | // 1) Templates / html = View
|
46 | 65 | // 2) Data that will populate our views = Models
|
47 | 66 | // 3) Logic to retrieve data, glue all views together and to react accordingly to user events,
|
48 | 67 | // data modifications, etc. = Controller
|
| 68 | +// |
| 69 | +// 在我们开始之间,我们先聊一下Flux存在的意义以及我们为什么需要它... |
| 70 | +// 假设我们正在构造一个web application. 一般的Web application是由什么构成的呢? |
| 71 | +// 1) Templates / html = View |
| 72 | +// 2) 数据将填充我们的Views = Models |
| 73 | +// 3) 检索数据的逻辑,将所有的View组织起来,并根据用户事件、数据修改做出相应的改变 = Controller |
49 | 74 |
|
50 | 75 | // This is the very classic MVC that we all know about. But it actually looks like concepts of flux,
|
51 | 76 | // just expressed in a slightly different way:
|
52 | 77 | // - Models look like stores
|
53 | 78 | // - user events, data modifications and their handlers look like
|
54 | 79 | // "action creators" -> action -> dispatcher -> callback
|
55 | 80 | // - Views look like React views (or anything else as far as flux is concerned)
|
| 81 | +// |
| 82 | +// 我们都知道,这是非常经典的MVC模型。但是这个Flux的概念还是有很多相似之处的,只是通过不同的方式表达了出来 |
| 83 | +// - Model 很像 stores |
| 84 | +// - 用户事件、数据变更已经他们的处理程序很像"action creators" -> action -> dispatcher -> callback |
| 85 | +// - Views就是React的View(对flux而言,可以是其他任何东西) |
56 | 86 |
|
57 | 87 | // So is flux just a matter of new vocabulary? Not exactly. But vocabulary DOES matter, because by introducing
|
58 | 88 | // these new terms we are now able to express more precisely things that were regrouped under
|
|
61 | 91 | // applications, we were just calling them differently. And instead of having handlers for those
|
62 | 92 | // actions directly modify Models or Views, flux ensures all actions go first through something called
|
63 | 93 | // a dispatcher, then through our stores, and finally all watchers of stores are notified.
|
| 94 | +// |
| 95 | +// 那么Flux只是科技人员创造的一个新词汇?这并不准确。但是新的词汇很重要,通过引入这个新词, |
| 96 | +// 我们可以重新集结一些新的术语来进行更准确的表达。举个例子:数据获取不就是一个action吗?就像点击也是一个action一样。 |
| 97 | +// 改变输入框中的值也是一个action。既然我们已经习惯了从我们的应用中发起action,我们只是用不同的方式调用他们。 |
| 98 | +// 在这些action修改Models或是Views之前,flux确保所有的actions先通过一个被称之为dispatcher的组件,再通过我们的stroes. |
| 99 | +// 最后,所有观察srotes的组件会被通知。 |
64 | 100 |
|
65 | 101 | // To get more clarity how MVC and flux differs, we'll
|
66 | 102 | // take a classic use-case in an MVC application:
|
|
69 | 105 | // 2) A click handler on button "A" triggers a change on Model "A"
|
70 | 106 | // 3) A change handler on Model "A" triggers a change on Model "B"
|
71 | 107 | // 4) A change handler on Model "B" triggers a change on View "B" that re-renders itself
|
| 108 | +// |
| 109 | +// 为了举例说明传统的MVC对象模型与Flux有哪些不同,我们来看一个典型的MVC应用程序: |
| 110 | +// 在这个典型模型中,你可以很简单的执行: |
| 111 | +// 1)用户点击按钮“A” |
| 112 | +// 2)在按钮“A”的单击处理程序中,触发了模块(model) “A”的变化 |
| 113 | +// 3)模块(model)“A”的变化处理程序中,触发了模块(model)“B”的变化 |
| 114 | +// 4)模块(model)“A”的变化处理程序中,触发了视图(View)“B”的重新渲染 |
72 | 115 |
|
73 | 116 | // Finding the source of a bug in such an environment when something goes wrong can become quite challenging
|
74 | 117 | // very quickly. This is because every View can watch every Model, and every Model can watch other Models, so
|
75 | 118 | // basically data can arrive from a lot of places and be changed by a lot of sources (any views or any models).
|
| 119 | +// |
| 120 | +// 在这种环境中,当出现问题时查找bug将会变得非常有挑战性。这是因为所有的View都可以观察每个model。 |
| 121 | +// 并且每个model也可以观察其他的model。所以基本数据将会从很多地方到达,并在很多代码中产生影响(任何的views或是models). |
76 | 122 |
|
77 | 123 | // Whereas when using flux and its unidirectional data flow, the example above could become:
|
78 | 124 | // 1) user clicks on button "A"
|
79 | 125 | // 2) a handler on button "A" triggers an action that is dispatched and produces a change on Store "A"
|
80 | 126 | // 3) since all other stores are also notified about the action, Store B can react to the same action too
|
81 | 127 | // 4) View "B" gets notified by the change in Stores A and B, and re-renders
|
| 128 | +// |
| 129 | +// 在上面的例子中使用Flux及单向数据流后: |
| 130 | +// 1)用户点击按钮“A” |
| 131 | +// 2)按钮“A”的单击处理程序触发一个dispatch的action.并产生对Store“A”的改变 |
| 132 | +// 3)由于其他所有的stores都被通知了相关的action.Store“B”也会对相同的action做出响应。 |
| 133 | +// 4)View“B”获得StroeA与StoreB的变化通知,并重新渲染 |
82 | 134 |
|
83 | 135 | // See how we avoid directly linking Store A to Store B? Each store can only be
|
84 | 136 | // modified by an action and nothing else. And once all stores have replied to an action,
|
85 | 137 | // views can finally update. So in the end, data always flows in one way:
|
86 | 138 | // action -> store -> view -> action -> store -> view -> action -> ...
|
| 139 | +// |
| 140 | +// 来看一下我们是如何避免Store A直接连接到Store B的?每一个store只能通过action来进行修改。 |
| 141 | +// 一旦所有的store相应同一个action后,views就被最终更新。所以一整套流程下来,数据总是单向的 |
| 142 | +// action -> store -> view -> action -> store -> view -> action -> ... |
87 | 143 |
|
88 | 144 | // Just as we started our use case above from an action, let's start our tutorial with
|
89 | 145 | // actions and action creators.
|
| 146 | +// |
| 147 | +// 正如我们上面由action开始,让我们也由actions及action creator来开启我们的教程 |
90 | 148 |
|
91 | 149 | // Go to next tutorial: simple-action-creator.js
|
0 commit comments