Skip to content

Commit e96d094

Browse files
committed
翻译0、1、2、3
1 parent d785ab9 commit e96d094

9 files changed

+154
-4
lines changed

.idea/encodings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jsLibraryMappings.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/redux_tutorial_node_modules.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/watcherTasks.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

00_introduction.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,22 @@
1111
// by other framework I tried before. I feel today that Redux is a much better way to learn about flux
1212
// than many other framework. That's why I want now to share with everyone, using my own words,
1313
// 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+
1426

1527
// You may have seen this diagram representing the famous unidirectional data flow of a flux application:
28+
//
29+
// 您可能已经看到过这个表示Flux的应用单向数据流的著名图表:
1630

1731
/*
1832
_________ ____________ ___________
@@ -39,20 +53,36 @@
3953
// to explain this complete diagram and the overall flow it describes, we'll take each piece separately and try to
4054
// understand why it exists and what role it plays. In the end you'll see that this diagram makes perfect sense
4155
// once we understood each of its parts.
56+
//
57+
// 在本教程中,我们将逐步向您介绍上图的概念。我们不试图完整的解释图表中数据流的描述,而是
58+
// 分别介绍每个部件并试图了解它为什么存在,它发挥什么样的作用。
59+
// 最后,一旦我们了解它的每一个部分,你会看到,这个图是非常合情合理的。
60+
//
4261

4362
// But before we start, let's speak a little bit about why flux exists and why we need it...
4463
// Let's pretend we're building a web application. What are all web applications made of?
4564
// 1) Templates / html = View
4665
// 2) Data that will populate our views = Models
4766
// 3) Logic to retrieve data, glue all views together and to react accordingly to user events,
4867
// 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
4974

5075
// This is the very classic MVC that we all know about. But it actually looks like concepts of flux,
5176
// just expressed in a slightly different way:
5277
// - Models look like stores
5378
// - user events, data modifications and their handlers look like
5479
// "action creators" -> action -> dispatcher -> callback
5580
// - 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而言,可以是其他任何东西)
5686

5787
// So is flux just a matter of new vocabulary? Not exactly. But vocabulary DOES matter, because by introducing
5888
// these new terms we are now able to express more precisely things that were regrouped under
@@ -61,6 +91,12 @@
6191
// applications, we were just calling them differently. And instead of having handlers for those
6292
// actions directly modify Models or Views, flux ensures all actions go first through something called
6393
// 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的组件会被通知。
64100

65101
// To get more clarity how MVC and flux differs, we'll
66102
// take a classic use-case in an MVC application:
@@ -69,23 +105,45 @@
69105
// 2) A click handler on button "A" triggers a change on Model "A"
70106
// 3) A change handler on Model "A" triggers a change on Model "B"
71107
// 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”的重新渲染
72115

73116
// Finding the source of a bug in such an environment when something goes wrong can become quite challenging
74117
// very quickly. This is because every View can watch every Model, and every Model can watch other Models, so
75118
// 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).
76122

77123
// Whereas when using flux and its unidirectional data flow, the example above could become:
78124
// 1) user clicks on button "A"
79125
// 2) a handler on button "A" triggers an action that is dispatched and produces a change on Store "A"
80126
// 3) since all other stores are also notified about the action, Store B can react to the same action too
81127
// 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的变化通知,并重新渲染
82134

83135
// See how we avoid directly linking Store A to Store B? Each store can only be
84136
// modified by an action and nothing else. And once all stores have replied to an action,
85137
// views can finally update. So in the end, data always flows in one way:
86138
// 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 -> ...
87143

88144
// Just as we started our use case above from an action, let's start our tutorial with
89145
// actions and action creators.
146+
//
147+
// 正如我们上面由action开始,让我们也由actions及action creator来开启我们的教程
90148

91149
// Go to next tutorial: simple-action-creator.js

01_simple-action-creator.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,72 @@
22

33
// We started to talk a little about actions in the introduction but what exactly are those "action creators"
44
// and how are they linked to "actions"?
5+
//
6+
// 在前言中,我们谈了一些actions的概念,那么究竟什么是“action creators”,他们又是怎么与“action”关联的呢?
57

68
// It's actually so simple that few lines of code can explain it all!
9+
//
10+
// 他实际上非常简单,只需要几行代码就可以解释一切了
711

812
// The action creator is just a function...
13+
//
14+
// action creator 仅仅是一个function而已..
915
var actionCreator = function() {
1016
// ...that creates an action (yeah the name action creator is pretty obvious now) and returns it
17+
// ... 创建(create) 一个 action(当然 action creator的名字就已经很明显了) 并返回他
1118
return {
1219
type: 'AN_ACTION'
1320
}
1421
}
1522

1623
// So is that all? yes.
24+
// 仅仅是这些?是的
1725

1826
// However one thing to note is the format of the action. This is kind of a convention in flux
1927
// that the action is an object that contains a "type" property. This type allow for further
2028
// handling of the action. Of course, the action can also contain other properties to
2129
// pass any data you want.
30+
//
31+
// 有一点要注意的是action的格式。在flux的约定中,action是一个包含 “type”属性的对象。这个“type”属性在action中允许
32+
// 被进一步处理。当然,action也包含其他属性来传递你需要的数据。
2233

2334
// We'll also see later that the action creator can actually return something else than an action,
2435
// like a function. This will be extremely useful for async action handling (more on that
2536
// in dispatch-async-action.js).
37+
//
38+
// 后期我们也会发现action creator可以返回除action以外的其他类型,比如function。这个功能在异步处理action时会
39+
// 显着非常有用(更多信息请参考 dispatch-async-action.js)
2640

2741
// We can call this action creator and get an action as expected:
42+
// 我们可以调用action creator来得到预期的action
2843
console.log(actionCreator())
2944
// Output: { type: 'AN_ACTION' }
3045

3146
// Ok, this works but it does not go anywhere...
3247
// What we need is to have this action to be sent somewhere so that
3348
// anyone interested could know that something happened and could act accordingly.
3449
// We call this process "Dispatching an action".
50+
//
51+
// 好了,已经正常工作了,但是并没有任何用处...
52+
// 我们需要的是将这个action发送到对它感兴趣的人那里,让他知道这件事情已经发生了,并做相应的处理。
53+
// 我们将这个过程称之为"Dispatching an action"。
54+
//
3555

3656
// To dispatch an action we need... a dispatch function ("Captain obvious").
3757
// And to let anyone interested know that an action happened, we need a mechanism to register
3858
// subscribers.
59+
//
60+
// 派发这个动作我们需要一个 dispatch function(“拼写就已经很明显了”)
61+
// 来让所有对这个动作感兴趣的人知道动作已经发生了。现在我们需要有一套注册订阅者(subscribers)的机制
3962

4063
// So far here is the flow of our application:
4164
// ActionCreator -> Action
65+
//
66+
// 总结下:目前为止我们的流程是:
67+
// ActionCreator -> Action
4268

4369
// Read more about actions and action creators here:
70+
// 在以下链接获取action creators更多的信息
4471
// http://gaearon.github.io/redux/docs/recipes/ReducingBoilerplate.html
4572

4673
// Go to next tutorial: simple-subscriber.js

02_simple-subscriber.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
// Tutorial 2 - simple-subscriber.js
22

33
// First things first, let's write a theoretical subscriber
4+
// 首先,我们先写一个理论上的订阅者
45

56
var mySubscriber = function() {
67
console.log('Something happened')
78
// do something
89
}
910

1011
// Simple isn't it?
12+
// 很简单是吧
1113
// It's still not registered anywhere but soon it will be.
14+
// 目前它仍然没有注册在任何地方,但是很快就可以了。
1215

1316
// And when called, it will do what it was designed for (here just a console.log)
17+
// 当他被调用时,他将完成在设计时赋予他的动作(例子中仅仅是打印一个 console.log)
1418

1519
mySubscriber()
1620

1721
// So far here is the flow of our application
1822
// ActionCreator -> Action ... Subscriber
23+
//
24+
// 目前为止 我们应用数据流向为:
25+
// ActionCreator -> Action ... Subscriber
1926

2027
// Go to next tutorial: about-state-and-meet-redux.js

03_about-state-and-meet-redux.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,60 @@
22

33
// Sometimes the actions that we'll handle in our application will not only inform us
44
// that something happened but also tell us that data needs to be updated.
5+
//
6+
// 在我们的应用中处理actions,有时不仅需要通知我们发生了,还需要告知我们哪些数据需要被改变。
57

68
// This is actually quite a big challenge in any apps.
79
// Where do I keep all the data regarding my application along its lifetime?
810
// How do I handle modification of such data?
911
// How do I propagate modifications to all parts of my application?
12+
//
13+
// 这对任何应用来说都是一个巨大的挑战
14+
// 我在哪里保持整个程序生命周期的数据?
15+
// 我要如何处理这些数据的修改?
16+
// 我怎么将修改传播到应用程序的所有部分?
1017

1118
// Here comes Redux.
19+
// 于是 Redux 出现了
1220

1321
// Redux (https://github.com/gaearon/redux) is a "predictable state container for JavaScript apps"
22+
// Redux (https://github.com/gaearon/redux) 是一个“针对JavaScript的应用程序可预见的状态容器”
1423

1524
// Let's review the above questions and reply to them with
1625
// Redux vocabulary (flux vocabulary too for some of them):
26+
//
27+
// 让我们回顾一下上面的问题,然后用Redux的词汇回答一下。(flux的词汇跟这个基本一致)
1728

1829
// Where do I keep all the data regarding my application along its lifetime?
30+
// 我在哪里保持整个程序生命周期的数据?
1931
// You keep it the way you want (JS object, array, Immutable structure, ...).
2032
// Data of your application will be called state. Makes sense since we're talking about
2133
// all the applications data that will evolve over time, it's really the applications state.
2234
// But you hand it over to Redux (Redux is "state container", remember?).
35+
// 你可以用你希望的形式保存(JS object,array,Immutable structure,).
36+
// 应用程序的数据将被称之为state.这是有道理的,因为我们正在谈论的所有应用程序的数据都会随着时间的推移而变化。
37+
// 它才是真正的application的状态(state)
38+
// 你把它交给了Redux处理(还记得吧,Redux被称作“状态容器”)
39+
//
2340
// How do I handle modification of such data?
41+
// 我要如何处理这些数据的修改?
2442
// Using reducers.
43+
// 使用 reducers.
2544
// A reducer is a subscriber to actions.
45+
// reducers 就是 actions的订阅者。
2646
// A reducer is just a function that receives the current state of your application, the action,
2747
// and returns a new state modified (or reduced as they call it)
48+
// reducers只是一个函数。他接受应用当前的state和action.并返回一个修改过的新state。
49+
// (或就像名字里描述的一样 一个减少了的state)
50+
//
2851
// How do I propagate modifications to all parts of my application?
52+
// 我怎么将修改传播到应用程序的所有部分
2953
// Using subscribers to state's modifications.
30-
54+
// 对state的改变使用订阅者
55+
//
3156
// Redux ties all this together for you.
57+
// Redux将这一切整合起来。
58+
//
3259
// To sum up, Redux will provide you:
3360
// 1) a place to put your application state
3461
// 2) a mechanism to subscribe to state updates

10_middleware.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
// Here is how it looks like (translated to es5 for readability):
4848

4949
var thunkMiddleware = function ({ dispatch, getState }) {
50-
// console.log('Enter thunkMiddleware');
50+
console.log('Enter thunkMiddleware');
5151
return function(next) {
52-
// console.log('Function "next" provided:', next);
52+
console.log('Function "next" provided:', next);
5353
return function (action) {
54-
// console.log('Handling action:', action);
54+
console.log('Handling action:', action);
5555
return typeof action === 'function' ?
5656
action(dispatch, getState) :
5757
next(action)

0 commit comments

Comments
 (0)