Skip to content

Commit

Permalink
fix(observer): mapActions should be mapped into 'methods' for component
Browse files Browse the repository at this point in the history
  • Loading branch information
EAGzzyCSL authored and jkzing committed Nov 14, 2017
1 parent 80f6701 commit 59e387c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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];
});
}
}
Expand Down Expand Up @@ -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') {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
2 changes: 1 addition & 1 deletion test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('utils', () => {
}).toThrow();
});
it('warn in console when warning condition fails', () => {
warning(false, '');
warning(true, '');
expect(console.warn).toHaveBeenCalled();
});
});

0 comments on commit 59e387c

Please sign in to comment.