UNPKG

3.9 kBMarkdownView Raw
1<h1 align="center">Revue</h1>
2
3<p align="center">
4 <a href="https://www.npmjs.com/package/revue">
5 <img src="https://camo.githubusercontent.com/b145895dcb12693255d3b4b371446ea6b73fa357/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f72657675652e737667" alt="NPM version" style="max-width:100%;">
6 </a>
7 <a href="https://www.npmjs.com/package/revue">
8 <img src="https://camo.githubusercontent.com/49a99ffd8da7a0793e1d648f859792e9b1db45fa/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f646d2f72657675652e737667" alt="NPM download" style="max-width:100%;"></a>
9 <a href="https://travis-ci.org/egoist/revue">
10 <img src="https://img.shields.io/travis/egoist/revue/master.svg" alt="Build Status" style="max-width:100%;">
11 </a>
12 <img src="https://img.shields.io/badge/stablity-experimental-red.svg">
13</p>
14
15Here is 2.0 branch, for version 1.0 please view [master](https://github.com/egoist/revue/tree/master) branch
16
17## Usage
18
19Obviously it works with Redux, install via NPM: `npm i -D redux revue`
20
21You can also hot-link the CDN version: https://npmcdn.com/revue/revue.js, `Revue` is exposed to `window` object.
22
23## The Gist
24
25You can try it online! http://esnextb.in/?gist=b300931ac26da8e9de2f
26
27**store.js**
28
29```js
30import Vue from 'vue'
31import Revue from 'revue'
32import {createStore} from 'redux'
33// create the logic how you would update the todos
34import todos from './reducers/todos'
35// create some redux actions
36import actions from './actions'
37
38// create a redux store
39const reduxStore = createStore(todos)
40// binding the store to Vue instance, actions are optional
41const store = new Revue(Vue, reduxStore, actions)
42// expose the store for your component to use
43export default store
44```
45
46**actions/todos.js**
47
48```js
49// create actionCreators yourself or use `redux-actions`
50export function addTodo(payload) {
51 return {type: 'ADD_TODO', payload}
52}
53export function toggleTodo(payload) {
54 return {type: 'TOGGLE_TODO', payload}
55}
56```
57
58**component.js**
59
60```js
61import store from './store'
62import * as todoActions from './actions/todo'
63
64export default {
65 data() {
66 return {
67 todo: '',
68 todos: store.state.todos
69 }
70 },
71 created() {
72 this.$subscribe('todos')
73 },
74 methods: {
75 addTodo() {
76 store.dispatch({type: 'ADD_TODO', this.todo})
77 // or use the actionCreator
78 store.dispatch(todoActions.addTodo(this.todo))
79 // also equal to: (if you binded actions when creating the store)
80 const {addTodo} = store.actions
81 store.dispatch(addTodo(this.todo))
82 }
83 }
84}
85```
86
87[**More detailed usages**](/example)
88
89## Hot-reload reducers
90
91Just change your `store.js` like this:
92
93Before:
94
95```javascript
96import { createStore } from 'redux'
97import rootReducer from './reducers'
98
99export default new Revue(Vue, createStore(rootReducer))
100```
101
102After:
103
104```javascript
105import { createStore } from 'redux'
106import rootReducer from './reducers'
107
108function configureStore() {
109 const reduxStore = createStore(rootReducer)
110 if (module.hot) {
111 module.hot.accept('./reducers', () => {
112 const nextRootReducer = require('./reducers').default
113 reduxStore.replaceReducer(nextRootReducer)
114 })
115 }
116 return reduxStore
117}
118
119export default new Revue(Vue, configureStore())
120```
121
122## FAQ
123
124**Do I have to use `this.$subscribe`? It's so verbose.**
125
126Yes. However the Redux store is a single immutable tree, you can think each property in the tree as a single store, to subscribe a state is like to subscribe its store (here we can call it a subscriber). I think Revue is kinda like [Alt.js](http://alt.js.org) somehow.
127
128## Development
129
130- **npm test** run unit test
131- **npm run watch** build example
132- **serve example** serve example
133- **make publish** publish a new version
134- **npm run cjs** build in CommonJS format
135- **npm run umd** build in umd format
136- **npm run demo** to publish demo to surge
137
138## License
139
140MIT &copy; [EGOIST](https://github.com/egoist)