From 59e387c67f9c5ffb3b009101fd036bdf90d829a2 Mon Sep 17 00:00:00 2001 From: EAGzzyCSL Date: Tue, 14 Nov 2017 18:20:29 +0800 Subject: [PATCH] fix(observer): mapActions should be mapped into 'methods' for component --- src/observer.js | 9 +++++---- src/utils.js | 2 +- test/utils.spec.js | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/observer.js b/src/observer.js index 22335c7..1358c3a 100644 --- a/src/observer.js +++ b/src/observer.js @@ -32,7 +32,7 @@ function reactionFn(mapState) { this.setData(mapped); } -function startMobxReaction(mapState, mapActions) { +function startMobxReaction(mapState, mapActions, isComponent) { const store = StoreMgr.getStore(); Object.defineProperty(this, '$store', { enumerable: false, @@ -46,11 +46,12 @@ function startMobxReaction(mapState, mapActions) { } if (mapActions) { + const actionMount = isComponent ? this.methods = this.methods || {} : this; let actions = mapActions(store, this.data) || {}; Object.keys(actions).forEach(name => { - warning(this[name] !== undefined, 'Trying to overwrite an existing property.'); + warning(actionMount[name] !== undefined, 'Trying to overwrite an existing property.'); assert(typeof actions[name] === 'function', 'Actions can only be function.'); - this[name] = actions[name]; + actionMount[name] = actions[name]; }); } } @@ -85,7 +86,7 @@ export default function observer(mapState, mapActions) { } else { const { onLoad, onUnload } = options; opts.onLoad = function() { - startMobxReaction.call(this, mapState, mapActions); + startMobxReaction.call(this, mapState, mapActions, isComponent); /* istanbul ignore else */ if (typeof onLoad === 'function') { diff --git a/src/utils.js b/src/utils.js index 4a7f099..dc49408 100644 --- a/src/utils.js +++ b/src/utils.js @@ -5,7 +5,7 @@ export function assert(condition, message) { export function warning(condition, message) { /* istanbul ignore else */ - if (!condition) { + if (condition) { console.warn(`[mobx-weapp]: ${message}`); } } \ No newline at end of file diff --git a/test/utils.spec.js b/test/utils.spec.js index 7a7d6ca..46e6d72 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -12,7 +12,7 @@ describe('utils', () => { }).toThrow(); }); it('warn in console when warning condition fails', () => { - warning(false, ''); + warning(true, ''); expect(console.warn).toHaveBeenCalled(); }); }); \ No newline at end of file