Git Product home page Git Product logo

easydog's Introduction

Easydog

Easydog 是一个前端状态管理工具,简化开发体验。适合小程序等项目等的开发。

查看demo

快速上手

创建一个 module

const app = {
  namespace: 'app',
  state: {
    past: [],
    current: 0,
    future: []
  },
  actions: {}
}

添加 actions

const app = {
  namespace: 'app',
  state: {
    past: [],
    current: 0,
    future: []
  },
  actions: {
    change ({ setState }, val) {
      const newPast = [...this.state.past, this.state.current]
      setState({
        past: newPast,
        current: val
      })
    },
    undo ({ setState }) {
      const { past, future, current } = this.state
      if (past.length === 0) { return }
      
      const previous = past.pop()
      const newFuture = [current, ...future]
      setState({
        past: [...past],
        current: previous,
        future: newFuture
      })
    },
    redo ({ setState }) {
      const { past, future, current } = this.state
      if (future.length === 0) {
        return
      }
      const next = future.shift()
      const newPast = [...past, current]
      setState({
        past: newPast,
        current: next,
        future: [...future]
      })
    }
  }
}

创建 Store

const store = createStore({ modules: app })

获取对状态读取和修改的方法

const states = store.mapStates({
  val: 'app/current'
})

const actions = store.mapActions({
  change: 'app/change',
  add (dispatch) {
    dispatch('app/change', states.val + 1)
    render('app/change')
  },
  reduce (dispatch) {
    dispatch('app/change', states.val - 1)
    render('app/change')
  },
  undo (dispatch) {
    dispatch('app/undo')
    render('app/undo')
  },
  redo (dispatch) {
    dispatch('app/redo')
    render('app/redo')
  }
})

调用方法

actions.add()

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.