Coder Social home page Coder Social logo

weappstore's Introduction

WeappStore

一个超轻量级的微信小程序状态管理库
An super lightweight state management tool for Weapp.

Redux太复杂了,有没有! 好吧,其实我只想同步一下全局变量所绑定的视图而已。那么试试这个轻量的状态管理工具把。

Usage

  1. 先下载WeappStore.js,并把它放在根目录下的utils目录里。
  2. 在app.js里,先初始化WeappStore,并把它存在app上。
var WeappStore = require('./utils/store.js')
// 创建store实例,并定义state,也就是希望在全局使用的状态变量。
var store = new WeappStore({
  userName: 'Rebecca',
  userEmail: undefined
})
// 可以开启debug模式
store.debug = true
// 把store存在app上
App({
  onLaunch: function () {},
  onError: function () {},
  $store: store
})
  1. 在需要使用全局状态变量的页面的onLoad函数里,将页面变量和全局状态变量进行连接。
var app = getApp()
Page({
  data: {},
  onLoad: function () {
    app.$store.link(this, 'userName')
  }
})
<view class="Page">
 Username: {{userName}}
</view>
  1. 好了,现在如果需要改变全局状态变量的时候,就可以这么写啦:
  app.$store.setState('userName', 'Alice')

所有绑定这个全局状态变量的页面,都会自动更新啦。

  1. 同时在页面的wxml也可以直接这么写
<view data-state=" userName: {{someVariable}} " catchtap="setState" />

因为在将页面link的时候,就自动为页面添加了setState方法,同时约定,data-state可以用于传递state的值。但是这里有一个限制,就是data-state只能是下面几种形式:

data-state=" userName: Alice "
// 事件函数里获得的state对象为
{
 userName: 'Alice'
}

data-state=" userNumber: 15 "
// 事件函数里获得的state对象为
{
 userNumber: 15
}

date-state=" userBool: false"
// 事件函数里获得的state对象为
{
 userBool: false
}

this.data.myVar = false
date-state=" userBool: {{myVar}}"
// 事件函数里获得的state对象为
{
 userBool: false
}

stringnumberboolean可以直接解析成对应的类型。

下面是几种错误的写法:

date-state=" {{userBool: myBoolVariable}}"
date-state=" userName: {{myNameVariable}}, userEmail: {{email}}"

目前在data-state里,仅支持一对键-值,如果需要设置更多的,那么可以在page里在写一个方法,在方法里再去设置state。 另外注意一点,在navigator上,不要用catchtap去绑定任何的事件,因为,页面跳转之后执行的新的页面的生命周期函数,和catchtap绑定的事件,是并行的。。。所以这是个坑,要注意。尽量用view + catchtap的方式去写。

很简单!有没有!

再重复一下,只有三个api

// 创建store实例
var weappStore = new WeappStore(stateObject)
// 将全局状态变量绑定到页面, 如果状态名空缺的话,则只会把为页面赋予setState的方法,这样页面相当于只能设置state。
weappStore.link(page, stateName='')
// 更新全局变量
weappStore.setState(stateName, newValue)

Redux什么的,暂时先放放吧。

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.