Skip to content

Commit

Permalink
Release 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ClickerMonkey committed Sep 9, 2018
0 parents commit f438e3d
Show file tree
Hide file tree
Showing 13 changed files with 1,402 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [],
"env": {
"amd": true
},
"rules": {
"no-new": 0
}
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
coverage/
npm-debug.log
package-lock.json
yarn.lock
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
nguage: node_js
node_js:
- 6.10.0
script: node_modules/karma/bin/karma start karma.conf.js --single-run
before_install:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
before_script:
- npm install
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Philip Diffenderfer <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
179 changes: 179 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
## vuex-router-actions

The library you've been waiting for to streamline complex Vuex actions and fast and secure routing in your app.

## Goals

- To make it easier to use Vuex actions.
- To make it easier to produce apps which can load data asynchronously before being routed to a page and also check along the way whether that user can go to that page - and also what to do when they cannot.
- To cache action results so traversing through an app is as efficient as possible.
- To make it easier to track asynchronous actions and keep the user notified when the app is loading.

## Features

- Cache the results of an action based on logic or a cache key.
- Listen to when actions are started, resolved, rejected, ended, and all pending actions are done.
- Call a mutation with a flag on whether a set of actions are currently running.
- Check the state of the store to determine whether an action can continue (subsequently a route).
- Keep a routed Vue component from loading or updating until an action is finished.
- If an action has exited because a user cannot visit a route, provide a way to redirect the user somewhere else.
- Communicate to the user when one or more actions are being processed (makes it easy to create loading screens).

## Contents

- [Goals](#goals)
- [Features](#features)
- [Dependencies](#dependencies)
- [Installation](#installation)
- [API](#api)
- [actionsWatch](#actionswatch)
- [actionsLoading](#actionsloading)
- [actionsCached](#actionscached)
- [actionsCachedConditional](#actionscachedconditional)
- [actionsProtect](#actionsprotect)
- [actionBeforeRoute](#actionbeforeroute)
- [actionOptional](#actionoptional)

### Dependencies

No build time dependencies, but this library is used in conjunction with `vuex` and optionally `vue-router`.

### Installation

#### npm

Installation via npm : `npm install --save vuex-router-actions`

## API

#### actionsWatch

Watches the given actions and invokes the callbacks passed in the options of `VuexRouterActions`. If the result of an action is a promise - it is watched and immediately invokes `onActionStart` followed by `onActionResolve` or `onActionReject` and then `onActionEnd`. If the result of an action is not a promise then `onActionStart` and `onActionEnd` are invoked immediately. If there are no more watched actions being executed then `onActionsDone` is invoked after the last `onActionEnd`.

```javascript
import VuexRouterActions, { actionsWatch } from 'vuex-router-actions'
const plugin = VuexRouterActions({
onActionStart (action, num) {},
onActionEnd (action, num, result, resolved) {}
})
const store = new Vuex.Store({
plugins: [plugin],
actions: {
...actionsWatch({
loadThis (context, payload) {},
loadThat (context, payload) {} // return a Promise
})
}
})
```

#### actionsLoading

Calls a mutation on a store when any of the passed actions are running and when they stop. This provides an easy way to signal to the user when something is loading, even a complex set of asynchronous actions.

```javascript
import { actionsLoading } from 'vuex-router-actions'
const store = new Vuex.Store({
state: {
loading: false
},
mutations: {
setLoading (state, loading) {
state.loading = loading
}
},
actions: {
...actionsLoading('setLoading', {
page1 (context, payload) {},
page2 (context, payload) {}
})
}
})
```

#### actionsCached

Produces actions with cached results based on some cache key. The action will always run the first time unless the cache key returned `undefined`. The cached results of the action are "cleared" when a different key is returned for a given action.

```javascript
import { actionsCached } from 'vuex-router-actions'
const store = new Vuex.Store({
actions: {
...actionsCached({
loadPage: {
getKey: (context, payload) => payload, // look at store, getters, payload, etc
handler: (context, payload) => null // some result that can be cached
}
})
}
})
```

#### actionsCachedConditional

Produces actions with cached results based on some condition. The action will always run the first time.

```javascript
import { actionsCachedConditional } from 'vuex-router-actions'
const store = new Vuex.Store({
actions: {
...actionsCachedConditional({
loadPage: {
isInvalid: (context, payload) => true, // look at store, getters, payload, etc
handler: (context, payload) => null // some result that can be cached
}
})
}
})
```

#### actionsProtect

Creates "protection" actions. These are functions which return a truthy or falsy value and the action returned produces a Promise that is resolved or rejected. This is useful when creating actions which load data for a route. You can add protection actions before and/or after the loading actions so it can validate the route path and afterwards validate whether the user should be able to see the given route. If the protect action returns another promise that promise is passed through, and whether it resolves or rejects determines if the action stops or continues.

```javascript
import { actionsProtect, actionBeforeRoute } from 'vuex-router-actions'
const store = new Vuex.Store({
actions: {
loadUser (context, user_id) {}, // returns promise which loads user data, also commits user to state
loadTask (context, task_id) {}, // returns promise which loads task data

// for actionBeforeRoute the to route is passed
loadTaskPage ({dispatch}, to) {
return dispatch('loadUser', to.params.user)
.then(user => dispatch('hasUser'))
.then(hasUser => dispatch('loadTask', to.params.task))
.then(task => dispatch('canEditTask', task))
},
...actionsProtect({
hasUser ({state}) { return state.user && !state.user.disabled },
canEditTask ({state}, task) { return state.user.canEdit( task ) }
})
}
})
// Task page
const component = {
...actionBeforeRoute('loadTaskPage')
}
```

#### actionBeforeRoute

Dispatches an action in the store and waits for the action to finish before the routed component this is placed in is entered or updated (see `beforeRouteEnter` and `beforeRouteUpdate` in `vue-router`). If the action resolves the routed component will be entered, otherwise `getOtherwise` will be invoked to determine where the router should be redirected. `getOtherwise` by default returns false which simply stops routing. If it returned undefined it would proceed as normal. If it returned a string or a route object it would redirect to that route.

```javascript
// MyPage.vue
import { actionBeforeRoute } from 'vuex-router-actions'
export default {
...actionBeforeRoute('loadMyPage', (to, from, rejectReason) => {
return '/path/i/can/goto/perhaps/previous/which/also/does/check'
})
}
```

#### actionOptional

Allows you to pass the results of a dispatch through this function and whether or not it resolves or rejects it won't stop from proceeding. This happens by returning a promise which always resolves. If the given promise is rejected then `resolveOnReject` is passed to the resolve function of the returned Promise.

## LICENSE
[MIT](https://opensource.org/licenses/MIT)
Loading

0 comments on commit f438e3d

Please sign in to comment.