0.8.0-alpha8 • Published 8 years ago

morpha-store v0.8.0-alpha8

Weekly downloads
4
License
MIT
Repository
-
Last release
8 years ago

Morpha-Store

A Redux Store for Morpha.

Guide

import React from 'react'
import ReactDOM from 'react-dom'
import { fetchList } from './apis'

import React from 'react'
import ReactDOM from 'react-dom'
import { fetchList } from './apis'

export default function morphaDemo({ store }) {
  let nextId = 0
  return {
    store: {
      initialState: {
        todos: [],
        other: 'other',
      },
      get: {
        length: ({ state }) => state.todos.length,
      },
      set: {
        addTodo({ state }, text, id) {
          return {
            ...state,
            todos: state.todos.concat({
              id: id || nextId ++,
              text,
            }),
          }
        },
        delTodo({ state }, id) {
          return {
            ...state,
            todos: state.todos.filter(todo => todo.id !== id),
          }
        },
      },
      action: {
        // 不并发...
        async fetchTodoList() {
          const dataList = await fetchList()
          dataList.forEach((data) => store.set('addTodo', data.text, data.id))
          return true
        },
      },
    },
    bootstrap() {
      store.setState({ todos: [{ id: 'init', text: 'abc' }] })
      // fetch action
      store.execAction('fetchTodoList')
    },
    render() {
      function mergeToProps({ state, setters, getters }) {
        return {
          todos: state.todos,
          addTodo: setters.addTodo,
          delTodo: setters.delTodo,
          length: getters.length(),
        }
      }
      // subscribe action
      const Container = store.connect(mergeToProps)(function TodosComponent({ todos, addTodo, delTodo, length }) {
        return (
          <div>
            <div>
              Totoal: {length}
            </div>
            <ul>
              {todos.map(todo => <li key={todo.id}>{todo.id}:{todo.text} <button onClick={() => delTodo(todo.id)}>Delete</button></li>)}
            </ul>
            <button onClick={() => addTodo('a new todo')}> AddTodo</button>
          </div>
        )
      })
      const el = document.createElement('div')
      document.body.appendChild(el)
      ReactDOM.render(<Container />, el)
    },
  }
}
0.8.0-alpha8

8 years ago

0.8.0-alpha7

8 years ago

0.8.0-alpha6

8 years ago

0.8.0-alpha5

8 years ago

0.8.0-alpha4

8 years ago

0.8.0-alpha3

8 years ago

0.8.0-alpha2

8 years ago

0.8.0-alpha

8 years ago

0.7.1-alpha9

9 years ago

0.7.1-alpha8

9 years ago

0.7.1-alpha7

9 years ago

0.7.1-alpha6

9 years ago

0.7.1-alpha5

9 years ago

0.7.1-alpha4

9 years ago

0.7.1-alpha3

9 years ago

0.7.1-alpha2

9 years ago

0.7.1-alpha1

9 years ago

0.7.1-alpha0

9 years ago

0.7.0

9 years ago

0.6.0

9 years ago

0.5.6

10 years ago

0.5.5

10 years ago

0.5.4

10 years ago

0.5.2

10 years ago

0.5.1

10 years ago

0.5.0

10 years ago

0.4.11

10 years ago

0.4.10

10 years ago

0.4.9

10 years ago

0.4.8

10 years ago

0.4.7

10 years ago

0.4.6

10 years ago

0.4.5

10 years ago

0.4.4

10 years ago

0.4.3

10 years ago

0.4.2

10 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.0

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago