Skip to content

Commit

Permalink
refactor(okam-core): change custom component trigger event normalize …
Browse files Browse the repository at this point in the history
…using beforeEmit hook and only for swan
  • Loading branch information
wuhy committed Nov 7, 2018
1 parent 8ab1e13 commit f965a2d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 45 deletions.
11 changes: 11 additions & 0 deletions packages/okam-core/src/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@

import {createComponent} from './helper/factory';
import {normalizeComponent} from './helper/component';
import {normalizeEventArgs} from './helper/triggerEvent';
import componentBase from './base/component';

/**
* Fix swan component event args
*
* @param {Array} args the event args
* @return {Array}
*/
componentBase.methods.__beforeEmit = function (args) {
return normalizeEventArgs(this, args);
};

/**
* Create component instance
*
Expand Down
44 changes: 1 addition & 43 deletions packages/okam-core/src/base/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,6 @@ import EventListener from '../util/EventListener';
import {getApis} from '../na/api';
import base from './base';

/**
* Normalize event arguments to fix the native swan framework bug
*
* @inner
* @param {Object} args the event args to normalize
* @return {Object}
*/
function normalizeEventArgs(args) {
let eventData = args[1];
if (eventData.currentTarget && eventData.target) {
return args;
}

let propData = this.properties;
let dataset = {};
propData && Object.keys(propData).forEach(k => {
if (/^data\w+$/.test(k)) {
let dataKey = k.replace(
/^data(\w)/,
(match, char) => char.toLowerCase()
);
dataset[dataKey] = propData[k];
}
});

let eventObj = {
type: args[0],
currentTarget: {
dataset,
id: this.id
},
target: {
dataset,
id: this.id
},
detail: eventData
};
args[1] = eventObj;

return args;
}

export default {

/**
Expand Down Expand Up @@ -118,7 +76,7 @@ export default {
* @param {...any} args the event arguments
*/
$emit(...args) {
args = normalizeEventArgs.call(this, args);
this.__beforeEmit && this.__beforeEmit(args);
this.$listener.emit.apply(this.$listener, args);

let triggerEvent = this.triggerEvent;
Expand Down
48 changes: 48 additions & 0 deletions packages/okam-core/src/helper/triggerEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @file Component triggerEvent helper
* @author [email protected]
*/

'use strict';

/**
* Normalize event arguments to fix the native swan framework bug
*
* @param {Component} component current component instance
* @param {Object} args the event args to normalize
* @return {Object}
*/
export function normalizeEventArgs(component, args) {
let eventData = args[1];
if (eventData.currentTarget && eventData.target) {
return args;
}

let propData = component.properties;
let dataset = {};
propData && Object.keys(propData).forEach(k => {
if (/^data\w+$/.test(k)) {
let dataKey = k.replace(
/^data(\w)/,
(match, char) => char.toLowerCase()
);
dataset[dataKey] = propData[k];
}
});

let eventObj = {
type: args[0],
currentTarget: {
dataset,
id: component.id
},
target: {
dataset,
id: component.id
},
detail: eventData
};
args[1] = eventObj;

return args;
}
23 changes: 21 additions & 2 deletions packages/okam-core/src/wx/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,24 @@

'use strict';

import Component from '../Component';
export default Component;
import {createComponent} from '../helper/factory';
import {normalizeComponent} from '../helper/component';
import componentBase from '../base/component';

/**
* Create component instance
*
* @param {Object} componentInfo the component information
* @param {Object} refComponents the component reference used in the component, the
* reference information is defined in the template
* @return {Object}
*/
export default function extendComponent(componentInfo, refComponents) {
return createComponent(
componentInfo,
componentBase,
normalizeComponent,
refComponents
);
}

0 comments on commit f965a2d

Please sign in to comment.