A powerful tool for communication between pages
Add mobx data layer driver to your applet
Current version: 0.2.0
Depends on mobx version: 4.9.2
The mini program already supports using npm to install third-party packages. For details, see npm support.
npm install wechat-weapp-mobx -S --production
Clone or download the code library locally:
git clone https://github.com/dao42/wechat-weapp-mbox
Copy the dist/mobx.js
, dist/diff.js
and dist/observer.js
files directly to the mini program project, for example (suppose below that we install all third-party packages in the libs directory):
cd wechat-weapp-mobx
cp mobx.js <小程序根目录> /libs
cp diff.js <小程序根目录> /libs
cp observer.js <小程序根目录> /libs
The above command copies the package to the mini program's <小程序根目录>/libs
directory.
Create a <小程序根目录>/stores
directory to store the data layer.
Create mobx stores
// <小程序根目录>/stores/todoStore.js
// 手动安装时引入的路径
// var extendObservable = require('../../libs/mobx').extendObservable;
// npm 包安装引入的路径
var extendObservable = require ( 'wechat-weapp-mobx/mobx' ) . extendObservable ;
var TodoStore = function ( ) {
extendObservable ( this , {
// observable data
todos : [ ] ,
todoText : 'aaa' ,
// computed data
get count ( ) {
return this . todos . length ;
}
} ) ;
// action
this . addTodo = function ( title ) {
this . todos . push ( { title : title } ) ;
}
this . removeTodo = function ( ) {
this . todos . pop ( ) ;
}
}
module . exports = {
default : new TodoStore ,
}
Bind page linkage events
// <小程序根目录>/pages/index/index.js
// 手动安装时引入的路径
// var observer = require('../libs/observer').observer;
// npm 包安装引入的路径
var observer = require ( 'wechat-weapp-mobx/observer' ) . observer ;
// 关键, 监控页面事件, 让 mobx 有机会更新页面数据
Page ( observer ( {
props : {
todoStore : require ( '../stores/todoStore' ) . default ,
} ,
// your other code below
onLoad : function ( ) {
}
} ) )
illustrate
After completing the above two steps, you can access it using props.todoStore
in wxml, and the data linkage will work automatically.
// <小程序根目录>/pages/index/index.wxml
< view > { { props . todoStore . todoText } } </ view >
Automatic data linkage
Data in stores can be accessed simultaneously across pages, and when the data is updated, the page will be automatically updated. This saves a lot of logic code.
Component ( observerComponment ( {
props : {
todoStore : require ( '../stores/todoStore' ) . default
}
} ) )
Please check the example directly: wechat-weapp-mobx-todos-npm
observerComponment
syntax in componentspage.data
in advance to avoid page rendering being too slow.Thanks to Danney for his contribution.
For detailed usage examples, please refer to: wechat-weapp-mobx-todos-npm
For the actual test version, please clone the following repo and use the mini program development tool to enable preview:
git clone https://github.com/dao42/wechat-weapp-mobx-todos-npm.git
For detailed usage examples, please refer to: wechat-weapp-mobx-todos
For the actual test version, please clone the following repo and use the mini program development tool to enable the preview:
git clone https://github.com/dao42/wechat-weapp-mobx-todos.git
Ballu - a real-time basketball scoring tool
Comment: This project is a very "complex" small program. Many pages of the project need to use websocket to synchronize data updates with the server. After using
wechat-weapp-mobx
as the data driver layer in depth, the problem of data status synchronization can be easily resolved. . The final project was successfully launched.
$ npm login --registry https://registry.npmjs.org/
$ npm publish
MIT