Skip to content

Commit

Permalink
feat(core): added global mixin api
Browse files Browse the repository at this point in the history
  • Loading branch information
dlhandsome authored and Gcaufy committed Sep 16, 2018
1 parent d784902 commit be65c8b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
17 changes: 16 additions & 1 deletion packages/core/dist/wepy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,8 @@ var config$1 = {
}
};

var globalMixinPatched = false;

var defaultStrat = function (parentVal, childVal) { return childVal ? childVal : parentVal; };
var strats = null;

Expand Down Expand Up @@ -2003,6 +2005,12 @@ function patchMixins (output, option, mixins) {
return;
}

if (!globalMixinPatched) {
var globalMixin = $global.mixin || {};
mixins = [].concat(mixins).concat(globalMixin);
globalMixinPatched = true;
}

if (isArr(mixins)) {
mixins.forEach(function (mixin) { return patchMixins(output, option, mixin); });
} else {
Expand Down Expand Up @@ -2090,6 +2098,12 @@ function use (plugin) {
plugin.installed = 1;
}

function mixin (options) {
if ( options === void 0 ) options = {};

$global.mixin = options;
}

var wepy = Base;

Object.assign(wepy, {
Expand All @@ -2099,7 +2113,8 @@ Object.assign(wepy, {
global: $global,

// global apis
use: use
use: use,
mixin: mixin
});

module.exports = wepy;
1 change: 1 addition & 0 deletions packages/core/weapp/apis/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './use';
export * from './mixin';
5 changes: 5 additions & 0 deletions packages/core/weapp/apis/mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import $global from '../global';

export function mixin (options = {}) {
$global.mixin = options;
}
9 changes: 9 additions & 0 deletions packages/core/weapp/init/mixins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { WEAPP_LIFECYCLE } from '../../shared/index';
import { patchProps } from './props';
import { patchData } from './data';
import { config } from '../config';
import $global from '../global';

let globalMixinPatched = false;

const defaultStrat = (parentVal, childVal) => childVal ? childVal : parentVal;
let strats = null;
Expand Down Expand Up @@ -44,6 +47,12 @@ export function patchMixins (output, option, mixins) {
return;
}

if (!globalMixinPatched) {
const globalMixin = $global.mixin || {};
mixins = [].concat(mixins).concat(globalMixin);
globalMixinPatched = true;
}

if (isArr(mixins)) {
mixins.forEach(mixin => patchMixins(output, option, mixin));
} else {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/weapp/wepy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import page from './page';
import app from './app';
import component from './component';
import $global from './global';
import { use } from './apis/index';
import { use, mixin } from './apis/index';


let wepy = Base;
Expand All @@ -15,7 +15,8 @@ Object.assign(wepy, {
global: $global,

// global apis
use
use,
mixin
});


Expand Down

0 comments on commit be65c8b

Please sign in to comment.