From 172f97cdd5e8b54e1059c02311057fe824fde7f9 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 7 Sep 2018 13:32:00 +0800 Subject: [PATCH] optimize code (#529) --- dist/mixins/basic.js | 5 ----- dist/mixins/button.js | 27 ++++++++++----------------- dist/notify/notify.js | 16 ++++++++-------- dist/steps/index.js | 12 +++--------- dist/steps/index.wxml | 2 +- dist/toast/toast.js | 5 ++++- dist/tree-select/index.js | 10 ++-------- dist/tree-select/index.wxml | 2 +- packages/mixins/basic.js | 5 ----- packages/mixins/button.js | 27 ++++++++++----------------- packages/notify/notify.js | 16 ++++++++-------- packages/steps/index.js | 12 +++--------- packages/steps/index.wxml | 2 +- packages/toast/toast.js | 5 ++++- packages/tree-select/index.js | 10 ++-------- packages/tree-select/index.wxml | 2 +- 16 files changed, 58 insertions(+), 100 deletions(-) diff --git a/dist/mixins/basic.js b/dist/mixins/basic.js index 8f7b21a1..3754e11a 100644 --- a/dist/mixins/basic.js +++ b/dist/mixins/basic.js @@ -1,9 +1,4 @@ export const basic = Behavior({ - options: { - multipleSlots: true, - addGlobalClass: true - }, - methods: { $emit() { this.triggerEvent.apply(this, arguments); diff --git a/dist/mixins/button.js b/dist/mixins/button.js index 1c72f1f8..fd15aea8 100644 --- a/dist/mixins/button.js +++ b/dist/mixins/button.js @@ -1,15 +1,12 @@ export const button = Behavior({ properties: { loading: Boolean, - // 在自定义组件中,无法与外界的 form 组件联动,暂时不开放 - // formType: String, openType: String, appParameter: String, - // 暂时不开放,直接传入无法设置样式 - // hoverClass: { - // type: String, - // value: 'button-hover' - // }, + sendMessageTitle: String, + sendMessagePath: String, + sendMessageImg: String, + showMessageCard: String, hoverStopPropagation: Boolean, hoverStartTime: { type: Number, @@ -26,32 +23,28 @@ export const button = Behavior({ sessionFrom: { type: String, value: '' - }, - sendMessageTitle: String, - sendMessagePath: String, - sendMessageImg: String, - showMessageCard: String + } }, methods: { bindgetuserinfo(event = {}) { - this.$emit('getuserinfo', event.detail || {}); + this.$emit('getuserinfo', event.detail); }, bindcontact(event = {}) { - this.$emit('contact', event.detail || {}); + this.$emit('contact', event.detail); }, bindgetphonenumber(event = {}) { - this.$emit('getphonenumber', event.detail || {}); + this.$emit('getphonenumber', event.detail); }, bindopensetting(event = {}) { - this.$emit('opensetting', event.detail || {}); + this.$emit('opensetting', event.detail); }, binderror(event = {}) { - this.$emit('error', event.detail || {}); + this.$emit('error', event.detail); } } }); diff --git a/dist/notify/notify.js b/dist/notify/notify.js index dd912e26..ef86de0c 100644 --- a/dist/notify/notify.js +++ b/dist/notify/notify.js @@ -1,25 +1,25 @@ +import { isObj } from '../common/utils'; + const defaultOptions = { selector: '#van-notify', duration: 3000 }; +function parseOptions(message) { + return isObj(message) ? message : { message }; +} + export default function Notify(options = {}) { const pages = getCurrentPages(); const ctx = pages[pages.length - 1]; - options = Object.assign({}, defaultOptions, parseParam(options)); + options = Object.assign({}, defaultOptions, parseOptions(options)); const el = ctx.selectComponent(options.selector); delete options.selector; if (el) { - el.setData({ - ...options - }); + el.setData(options); el.show(); } } - -function parseParam(params = '') { - return typeof params === 'object' ? params : { text: params }; -} diff --git a/dist/steps/index.js b/dist/steps/index.js index 29587de4..777a3175 100644 --- a/dist/steps/index.js +++ b/dist/steps/index.js @@ -28,16 +28,10 @@ create({ methods: { formatSteps() { const { steps } = this.data; - const formattedSteps = steps.map((step, index) => { - return { - ...step, - status: this.getStatus(index) - }; - }); - - this.setData({ - formattedSteps + steps.forEach((step, index) => { + step.status = this.getStatus(index); }); + this.setData({ steps }); }, getStatus(index) { diff --git a/dist/steps/index.wxml b/dist/steps/index.wxml index 5c39b968..01f9f534 100644 --- a/dist/steps/index.wxml +++ b/dist/steps/index.wxml @@ -1,6 +1,6 @@ isObj(message) ? message : { message }; let queue = []; let currentOptions = { ...defaultOptions }; +function parseOptions(message) { + return isObj(message) ? message : { message }; +} + function Toast(options = {}) { options = { ...currentOptions, diff --git a/dist/tree-select/index.js b/dist/tree-select/index.js index 63a6643a..5a433cb7 100644 --- a/dist/tree-select/index.js +++ b/dist/tree-select/index.js @@ -39,18 +39,12 @@ create({ methods: { // 当一个子项被选择时 onSelectItem(event) { - const { - dataset = {} - } = event.currentTarget || {}; - this.$emit('click-item', { ...(dataset.item || {}) }); + this.$emit('click-item', event.currentTarget.dataset.item); }, // 当一个导航被点击时 onClickNav(event) { - const { - dataset = {} - } = event.currentTarget || {}; - this.$emit('click-nav', { index: dataset.index }); + this.$emit('click-nav', event.currentTarget.dataset.index); }, // 更新子项列表 diff --git a/dist/tree-select/index.wxml b/dist/tree-select/index.wxml index 6344dc34..fea23c47 100644 --- a/dist/tree-select/index.wxml +++ b/dist/tree-select/index.wxml @@ -19,7 +19,7 @@ > { - return { - ...step, - status: this.getStatus(index) - }; - }); - - this.setData({ - formattedSteps + steps.forEach((step, index) => { + step.status = this.getStatus(index); }); + this.setData({ steps }); }, getStatus(index) { diff --git a/packages/steps/index.wxml b/packages/steps/index.wxml index 5c39b968..01f9f534 100644 --- a/packages/steps/index.wxml +++ b/packages/steps/index.wxml @@ -1,6 +1,6 @@ isObj(message) ? message : { message }; let queue = []; let currentOptions = { ...defaultOptions }; +function parseOptions(message) { + return isObj(message) ? message : { message }; +} + function Toast(options = {}) { options = { ...currentOptions, diff --git a/packages/tree-select/index.js b/packages/tree-select/index.js index 63a6643a..5a433cb7 100644 --- a/packages/tree-select/index.js +++ b/packages/tree-select/index.js @@ -39,18 +39,12 @@ create({ methods: { // 当一个子项被选择时 onSelectItem(event) { - const { - dataset = {} - } = event.currentTarget || {}; - this.$emit('click-item', { ...(dataset.item || {}) }); + this.$emit('click-item', event.currentTarget.dataset.item); }, // 当一个导航被点击时 onClickNav(event) { - const { - dataset = {} - } = event.currentTarget || {}; - this.$emit('click-nav', { index: dataset.index }); + this.$emit('click-nav', event.currentTarget.dataset.index); }, // 更新子项列表 diff --git a/packages/tree-select/index.wxml b/packages/tree-select/index.wxml index 6344dc34..fea23c47 100644 --- a/packages/tree-select/index.wxml +++ b/packages/tree-select/index.wxml @@ -19,7 +19,7 @@ >