Skip to content

Commit bcd3ce6

Browse files
author
Jack Ellis
committed
encase
1 parent b6b8281 commit bcd3ce6

File tree

5 files changed

+302
-175
lines changed

5 files changed

+302
-175
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,40 @@ By default this will create a brand new injector, but if you want to share regis
229229
#### strict
230230
If set to `false` then all dependencies will be made optional. If a component's dependency cannot be found, rather than throwing an error it will just be set to `undefined`. Not that this does not affect the `get` function.
231231

232+
#### encase
233+
```js
234+
(
235+
dependencies?: Array<string>,
236+
fn: (...deps) => Function
237+
)
238+
```
239+
Encase allows you to wrap any function in an outer function. This allows you to inject dependencies (at runtime) but the original function signature will remain the same.
240+
241+
In the context of a Vue application, this method comes into its own when writnig Vuex actions. For example, the following action:
242+
243+
```js
244+
import axios from 'axios';
245+
246+
const actions = {
247+
FETCH: ({ commit }) => {
248+
axios.get('/my/api').then((response) => {
249+
commit('FETCHED', response.data);
250+
});
251+
},
252+
};
253+
```
254+
can be rewritten as:
255+
```js
256+
const actions = {
257+
FETCH: encase([ 'axios' ], (axios) => ({ commit }) => {
258+
axios.get('/my-api').then((response) => {
259+
commit('FETCHED', response.data);
260+
});
261+
}),
262+
};
263+
```
264+
now this does add a little more code to the function, but it means we've got proper dependency injection per action! And Vuex doesn't even need to know about it.
265+
232266
### Lifecycle
233267
When registering a factory or service, it's possible to determine the lifecycle.
234268
*As of v0.4, the default lifecycle is set to `class`*.

changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 2.1.0
2+
- Added `encase` method
3+
14
## 2.0.1
25
- Fixed an issue where doing `Vue.extend().use(vueInject)` meant vue inject could not find `optionMergeStrategies`
36

0 commit comments

Comments
 (0)