-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 1 commit
2ad1901
11e1e29
131acb2
65f8e11
f8d117f
ccdc719
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
- Les actions peuvent contenir des opérations asynchrones. | ||
|
||
Let's register a simple action: | ||
Enregistrons une simple action : | ||
|
||
``` js | ||
const store = new Vuex.Store({ | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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: { | ||
|
@@ -37,15 +37,15 @@ actions: { | |
} | ||
``` | ||
|
||
### Dispatching Actions | ||
### Dispatcher des actions dans les composants | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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: { | ||
|
@@ -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* : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
``` js | ||
// dispatch with a payload | ||
// dispatcher avec un payload | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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** : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
``` 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
### Dispatching Actions in Components | ||
### Dispatcher des actions dans les composants | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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) : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
``` js | ||
import { mapActions } from 'vuex' | ||
|
@@ -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 : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
``` js | ||
actions: { | ||
|
@@ -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: { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
actions: { | ||
async actionA ({ commit }) { | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.