Skip to content

Traduction de actions.md #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 23, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 29 additions & 33 deletions docs/en/actions.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Actions

Actions are similar to mutations, the difference being that:
Les actions sont similaires aux mutations, à la différence que :

- Instead of mutating the state, actions commit mutations.
- Actions can contain arbitrary asynchronous operations.
- Au lieu de modifier le state, les actions committent des mutations.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modifier l'état

actent des

- Les actions peuvent contenir des opérations asynchrones.

Let's register a simple action:
Enregistrons une simple action :

``` js
const store = new Vuex.Store({
Expand All @@ -25,9 +25,9 @@ const store = new Vuex.Store({
})
```

Action handlers receive a context object which exposes the same set of methods/properties on the store instance, so you can call `context.commit` to commit a mutation, or access the state and getters via `context.state` and `context.getters`. We will see why this context object is not the store instance itself when we introduce [Modules](modules.md) later.
Les handlers d'action reçoivent un objet contexte qui expose le même set de méthodes/propriétés que l'instance du store, donc vous pouvez appeler `context.commit` pour commiter une mutation, ou accéder au state et aux getters via `context.state` et `context.getters`. Nous verrons pourquoi cet objet contexte n'est pas l'instance du store elle-même lorsque nous présenterons les [Modules](moduels.md) plus tard.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

les gestionnaires d'action

les mêmes méthodes/propriétés

acter une mutation

accéder à l'état et aux accesseurs

modules.md

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expose le même ensemble de méthodes/propriétés

elle-même, lorsque


In practice, we often use ES2015 [argument destructuring](https://github.com/lukehoban/es6features#destructuring) to simplify the code a bit (especially when we need to call `commit` multiple times):
En pratique, nous utilisons souvent la [destructuration d'argument](https://github.com/lukehoban/es6features#destructuring) (*argument destructuring*) pour simplifier quelque peu le code (particulièrement si nous avons besoin d'appeler `commit` plusieurs fois) :

``` js
actions: {
Expand All @@ -37,15 +37,15 @@ actions: {
}
```

### Dispatching Actions
### Dispatcher des actions dans les composants
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Propager des


Actions are triggered with the `store.dispatch` method:
Les actions sont déclenchées par la méthode `store.dispatch` :

``` js
store.dispatch('increment')
```

This may look dumb at first sight: if we want to increment the count, why don't we just call `store.commit('increment')` directly? Well, remember that **mutations must be synchronous**? Actions don't. We can perform **asynchronous** operations inside an action:
Cela peut sembler idiot au premier abord : si nous avons besoin d'incrémenter le compteur, pourquoi ne pas simplement appeler `store.commit('increment')` directement ? Et bien, vous rappelez-vous que **les mutations doivent être synchrones** ? Les actions ne suivent pas cette règle. Il est possible de procéder à des opérations **asynchrones** dans une action :

``` js
actions: {
Expand All @@ -57,32 +57,31 @@ actions: {
}
```

Actions support the same payload format and object-style dispatch:
Les actions prennent en charge le même format de payload et *object-style dispatch* :
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

également en charge les paramètres additionnels (« payload ») et les objets pour propager :


``` js
// dispatch with a payload
// dispatcher avec un payload
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

propager avec un paramètre additionnel

store.dispatch('incrementAsync', {
amount: 10
})

// dispatch with an object
// dispatcher avec un object
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

propager avec un objet

store.dispatch({
type: 'incrementAsync',
amount: 10
})
```

A more practical example of real-world actions would be an action to checkout a shopping cart, which involves **calling an async API** and **committing multiple mutations**:
Un exemple plus pratique d'une application du monde réel serait une action pour check-out un panier d'achats, ce qui implique **d'appeler une API asynchrone** et de **comitter de multiples mutations** :
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Un exemple concret d'application serait

pour vider un panier d'achats

acter de multiples


``` js
actions: {
checkout ({ commit, state }, products) {
// save the items currently in the cart
// sauvegarder les articles actuellement dans le panier
const savedCartItems = [...state.cart.added]
// send out checkout request, and optimistically
// clear the cart
// envoyer la requête de checkout, et vider le panier
commit(types.CHECKOUT_REQUEST)
// the shop API accepts a success callback and a failure callback
// l'API du shop prend un callback success et un callback failure
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// l'API de la boutique en ligne prend une fonction de rappel en cas de succès et une en cas d'échec

shop.buyProducts(
products,
// handle success
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gérer le succès

gérer l'échec

Expand All @@ -94,11 +93,11 @@ actions: {
}
```

Note we are performing a flow of asynchronous operations, and recording the side effects (state mutations) of the action by committing them.
Notez que nous procédons à un flux d'opérations asynchrones, et enregistrons les effets de bord (mutation du state) de l'action en les committant.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutation de l'état

les actants

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

les actant


### Dispatching Actions in Components
### Dispatcher des actions dans les composants
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Propager


You can dispatch actions in components with `this.$store.dispatch('xxx')`, or use the `mapActions` helper which maps component methods to `store.dispatch` calls (requires root `store` injection):
Vous pouvez dispatcher des actions dans les composants avec `this.$store.dispatch('xxx')`, ou en utilisant le helper `mapActions` qui attache les méthodes du composant aux appels de `store.dispatch` (nécessite l'injection de `store` à la racine) :
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pouvez propager

la fonction utilitaire


``` js
import { mapActions } from 'vuex'
Expand All @@ -107,23 +106,20 @@ export default {
// ...
methods: {
...mapActions([
'increment', // map this.increment() to this.$store.dispatch('increment')

// mapActions also supports payloads:
'incrementBy' // this.incrementBy(amount) maps to this.$store.dispatch('incrementBy', amount)
'increment' // attacher this.increment() à this.$store.dispatch('increment')
]),
...mapActions({
add: 'increment' // map this.add() to this.$store.dispatch('increment')
add: 'increment' // attacher this.add() à this.$store.dispatch('increment')
})
}
}
```

### Composing Actions
### Composer les actions

Actions are often asynchronous, so how do we know when an action is done? And more importantly, how can we compose multiple actions together to handle more complex async flows?
Les actions sont souvent asynchrones, donc comment savoir lorsqu'une action est terminée ? Et plus important, comment composer plusieurs actions ensemble pour manipuler des flux asynchrones plus complexes ?

The first thing to know is that `store.dispatch` can handle Promise returned by the triggered action handler and it also returns Promise:
La première chose à savoir est que `store.dispatch` retourne la valeur retournée par le handler de l'action déclenchée, vous pouvez donc retourner une Promise :
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

peut gérer la Promesse (« Promise ») retournée par le gestionnaire d'action déclenché et par conséquent vous pouvez également retourner une Promesse :


``` js
actions: {
Expand All @@ -138,15 +134,15 @@ actions: {
}
```

Now you can do:
Maintenant vous pouvez faire :

``` js
store.dispatch('actionA').then(() => {
// ...
})
```

And also in another action:
Et également dans une autre action :

``` js
actions: {
Expand All @@ -159,10 +155,10 @@ actions: {
}
```

Finally, if we make use of [async / await](https://tc39.github.io/ecmascript-asyncawait/), a JavaScript feature landing very soon, we can compose our actions like this:
Pour finir, nous pouvons utiliser de [async / await](https://tc39.github.io/ecmascript-asyncawait/), une fonctionnalité JavaScript qui sera disponible très bientôt, nous pouvons composer nos actions ainsi :

``` js
// assuming getData() and getOtherData() return Promises
// sachant que getData() et getOtherData() retournent des Promises
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getData() et getOtherData()

Promesses


actions: {
async actionA ({ commit }) {
Expand All @@ -175,4 +171,4 @@ actions: {
}
```

> It's possible for a `store.dispatch` to trigger multiple action handlers in different modules. In such a case the returned value will be a Promise that resolves when all triggered handlers have been resolved.
> Il est possible pour un `store.dispatch` de déclencher plusieurs handlers d'action dans différents modules. Dans ce genre de cas, la valeur retournée sera une Promise qui se résoud quand tous les handlers déclenchés ont été résolus.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gestionnaires d'action

une Promesse

gestionnaires déclenchés