Skip to content

Commit

Permalink
feat(redux): bind mapState to context
Browse files Browse the repository at this point in the history
  • Loading branch information
Genuifx committed Apr 17, 2019
1 parent 8f71a7f commit 43143cf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/wxa-redux/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let mountRedux = function (originHook) {
let connectState = ()=>{
let newState = this.$store.getState();
let source = this.$$storeLastState;
let data = mapState(this.mapState, newState, source);
let data = mapState(this.mapState, newState, source, this);

if (data !== null) {
// 有效state
Expand Down Expand Up @@ -77,7 +77,7 @@ export const wxaRedux = (options = {}) => {
vm.onLoad = mountRedux(onLoad);
vm.onShow = function (...args) {
this.$$isCurrentPage = true;
let data = mapState(this.mapState, this.$store.getState(), this.$$storeLastState);
let data = mapState(this.mapState, this.$store.getState(), this.$$storeLastState, this);
if (data != null) {
let diffData = this.$$reduxDiff(data)
this.setData(diffData)
Expand Down
6 changes: 5 additions & 1 deletion packages/wxa-redux/src/mapState.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import shallowequal from 'shallowequal'

export default function mapState(map, state, source={}) {
export default function mapState(map, state, source={}, context) {
if(map == null) return null;

let {newState, oldState} = Object.keys(map).reduce((ret, key)=>{
if(typeof map[key] !== 'function') {
console.log(`mapState中的${key}必须为函数`);
return ret;
}

try {
if (context) map[key].bind(context);

ret.newState[key] = map[key](state);
ret.oldState[key] = map[key](source);
} catch(e) {
Expand Down

0 comments on commit 43143cf

Please sign in to comment.