Skip to content

Commit

Permalink
fixed mixin customize function issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Feb 20, 2017
1 parent 937af50 commit 532bcd3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/wepy/src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ export default class {
init (parent) {
let k;

// 自定义方法与属性覆盖
Object.getOwnPropertyNames(this).forEach((k) => {
if (k[0] + k[1] !== 'on') {
if (!parent[k])
parent[k] = this[k];
}
// 自定义属性覆盖
Object.getOwnPropertyNames(this)
.concat(Object.getOwnPropertyNames(Object.getPrototypeOf(this)))
.forEach((k) => {
if (k[0] + k[1] !== 'on' && k !== 'constructor') {
if (!parent[k])
parent[k] = this[k];
}
});


// 数据,事件,组件覆盖
['data', 'events', 'components'].forEach((item) => {
Object.getOwnPropertyNames(this[item]).forEach((k) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/wepy/test/fake/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ module.exports = class Mix extends wepy.mixin {
};
}

func1 () {
return 'mixin-func1';
}

func2 () {
return 'mixin-func2';
}

onLoad() {

}
Expand Down
5 changes: 5 additions & 0 deletions packages/wepy/test/fake/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ module.exports = class Index extends wepy.page {
assert.strictEqual(this instanceof Index, true, 'page custom method triggered, this is self');
assert.strictEqual(arg, 'user_custom', 'page custom method triggered with params');
}


func1 () {
return 'parent-func1';
}
}
20 changes: 20 additions & 0 deletions packages/wepy/test/mixin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

var assert = require('assert');
var wepy = require('../lib/wepy.js').default;
var mixin = require('../lib/mixin.js').default;
var wxfake = require('./wxfake');
var Index = require('./fake/page');
var App = require('./fake/app');


describe('mixin.js', () => {
Expand Down Expand Up @@ -41,4 +45,20 @@ describe('mixin.js', () => {
assert.strictEqual(parent.data.c, 1, 'mixin data copied if not exsit in parent.');
});


it('mixin function', () => {
let appConfig = wepy.$createApp(App);
let pageConfig = wepy.$createPage(Index);

let page = pageConfig.$page;
let app = appConfig.$app;

pageConfig.onLoad.call(wxfake.getWxPage());


assert.strictEqual(page.func1(), 'parent-func1', 'mixin customize function wont copy if parent function exsit.');
assert.strictEqual(page.func2(), 'mixin-func2', 'mixin customize function will copy if parent function exsit.');

});

});

0 comments on commit 532bcd3

Please sign in to comment.