Giter Site home page Giter Site logo

mobx-wxapp's Introduction

mobx-wxapp

在小程序中使用mobxconnect函数可将被观察的数据高效的绑定到小程序视图。 旧的inject方式见v1

安装

npm install mobx-wxapp 安装项目到本地 或 直接拷贝文件(example/mobx-wxapp.js)到您的项目。

(案例使用了 mobx.js v4.6.0 ,因 mobx5 使用了小程序暂不支持的 ES6 proxy)

用法

pages/index.js:

import { connect, extract } from '../mobx-wxapp'
import { observable } from '../mobx'

const appStore = observable({
  title: 'mobx-wxapp'
})

const store = observable({

  // observable
  seconds: 0,

  // computed
  get color() {
    return this.seconds % 2 ? 'red' : 'green'
  },

  // actions
  tick() {
    this.seconds += 1
  }
})

// page
Page({
  onLoad() {
    connect(this, () => ({
        title: appStore.title,
        color: store.color,
        seconds: store.seconds
        // 或者使用 extract 一次性提取全部属性
        // ...extract(store)
      })
    )
  },
  add() {
    store.tick()
  }
})

pages/index.wxml:

<view>{{ title }} :</view>
<view style="color:{{ color }}"> seconds:{{ seconds }} </view>
<button bindtap="add">add</button>

当然,您也可在 Component 中使用:

Component({
  //..
  ready(){
    this.disposer = connect(this,mapDataToStore,options)
  },

  //请务必在组件生命周期结束前执行销毁器!
  detached(){
    this.disposer();
  }
  //...
})

API

connect(context,mapDataToStore,options?)

  • context:Object // 请传入this
  • mapDataToStore:Function //需要绑定到视图的映射函数
  • options:Object // 可选参数
options = {
  delay:Number,// setData的最小执行间隔,默认30ms
  setDataCallback:Function // setData的执行回调
}

返回值:connect返回一个销毁器函数(在 Page 中使用时将自动在 onUnload 生命周期执行,但在 Component 构造器中使用时请确保在生命周期结束时手动调用此函数)。

extract(store)

映射整个store的快捷方式

  • store:Object // 一个被观察的store对象

返回值:一个可被映射到data的对象

应用

License

ISC licensed.

mobx-wxapp's People

Contributors

lbqdhx avatar lbqdly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mobx-wxapp's Issues

mobx.js

项目里的 mobx.js 文件做过什么修改吗,可以用 mobx 官方项目里的最新 4.x 版替换吗?

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.