Giter Club home page Giter Club logo

revue's Introduction

Revue

NPM version NPM download Build Status

Usage

New: Time to use Redux-devtools in non-React apps!

Obviously it works with Redux, install via NPM: npm i -D redux revue

You can also hot-link the CDN version: https://npmcdn.com/revue/revue.js, Revue is exposed to window object.

// App.js
import Revue from 'revue'
import store from './store'
import actions from './actions'
Vue.use(Revue, {
  store,
  // if you want to call your actions from vm instance
  // then your vm you can call like `this.$actions.addTodo(todo)`
  actions
})

// store.js
// just put some reducers in `./reducers` like
// what you do in pure Redux
// and combine them in `./reducers/index.js`
import { createStore } from 'redux'
import reducer from './reducers/index'
export default createStore(reducer)

// component.js
// some component using Revue
new Vue({
  el: '#app',
  data () {
    return {
      counter: this.$store.state.counter
    }
  },
  ready () {
    // subscribe state changes
    this.$subscribe('counter')
    // if your name the 'counter' to 'temp_counter' in data()
    // you can use this.$subscribe('counter as temp_counter')
    // if you want to subscribe a deep property
    // this.$subscribe('top.middle.counter as counter')
    // or even this.$subscribe('something.in.reduxStore.counter as instance.somewhere.counter')
    // you can only $subscribe once, if you want to subscribe multi states at the same time, do this:
    /*
    this.$subscribe(
      'foo',
      'bar'
    )
    */
  },
  methods: {
    handleClickCounter () {
      // dispatch events
      this.$store.dispatch({type: 'INCREMENT'})
    }
  }
})

More detailed usages

Hot-reload reducers

Just change your store.js like this:

Before:

import { createStore } from 'redux'
import rootReducer from './reducers'

export default createStore(rootReducer)

After:

import { createStore } from 'redux'
import rootReducer from './reducers'

function configureStore() {
  const store = createStore(rootReducer)
  if (module.hot) {
    module.hot.accept('./reducers', () => {
      const nextRootReducer = require('./reducers').default
      store.replaceReducer(nextRootReducer)
    })
  }
  return store
}

export default configureStore()

FAQ

Do I have to use this.$subscribe? It's so verbose.

No, not always if you don't care about mutating states in reducers. And also because Vue states are mutable and observable, it's ok for you to modify data directly like state.foo = 'bar', then it becomes so similar to the Vue Flux implementation Vuex, it allows you to mutate data. However what the best part of Redux is states are immutable, which means you can't make direct operations on states so that you have less chance to make mistakes.

this.$subscribe is only needed if you don't mutate states directly. And you're recommended to do so.

License

MIT © EGOIST

revue's People

Contributors

christian-fei avatar egoist avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.