From 99335593427ffb6dfef833ac5e1e35d1ab68e6c7 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Mon, 8 Apr 2019 17:00:54 +0800 Subject: [PATCH] feat(runtime): triggerEvent --- package.json | 3 +- packages/uni-app-plus/dist/index.js | 71 +++++++++++++++++++++-- packages/uni-app-plus/package.json | 2 +- packages/uni-mp-weixin/dist/index.js | 71 +++++++++++++++++++++-- packages/uni-mp-weixin/package.json | 2 +- src/core/runtime/wrapper/create-app.js | 7 ++- src/core/runtime/wrapper/util.js | 2 +- src/platforms/app-plus/runtime/index.js | 1 + src/platforms/mp-alipay/runtime/index.js | 0 src/platforms/mp-baidu/runtime/index.js | 0 src/platforms/mp-toutiao/runtime/index.js | 0 src/platforms/mp-weixin/runtime/index.js | 47 +++++++++++++++ src/shared/util.js | 21 ++++++- 13 files changed, 212 insertions(+), 15 deletions(-) create mode 100644 src/platforms/app-plus/runtime/index.js create mode 100644 src/platforms/mp-alipay/runtime/index.js create mode 100644 src/platforms/mp-baidu/runtime/index.js create mode 100644 src/platforms/mp-toutiao/runtime/index.js create mode 100644 src/platforms/mp-weixin/runtime/index.js diff --git a/package.json b/package.json index 951742e64..0e017e0ff 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,8 @@ "globals": { "App": true, "Page": true, - "Component": true, + "Component": true, + "Behavior":true, "getApp": true, "getCurrentPages": true, "plus": true, diff --git a/packages/uni-app-plus/dist/index.js b/packages/uni-app-plus/dist/index.js index 4758dae3d..5308b4af1 100644 --- a/packages/uni-app-plus/dist/index.js +++ b/packages/uni-app-plus/dist/index.js @@ -19,7 +19,26 @@ function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } -function noop () {} +function noop () {} + +/** + * Create a cached version of a pure function. + */ +function cached (fn) { + const cache = Object.create(null); + return function cachedFn (str) { + const hit = cache[str]; + return hit || (cache[str] = fn(str)) + } +} + +/** + * Camelize a hyphen-delimited string. + */ +const camelizeRE = /-(\w)/g; +const camelize = cached((str) => { + return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') +}); const SYNC_API_RE = /requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$/; @@ -241,6 +260,49 @@ var api = /*#__PURE__*/Object.freeze({ requireNativePlugin: requireNativePlugin }); +const WXPage = Page; +const WXComponent = Component; + +const customizeRE = /:/g; + +const customize = cached((str) => { + return camelize(str.replace(customizeRE, '-')) +}); + +function initTriggerEvent (mpInstance) { + const oldTriggerEvent = mpInstance.triggerEvent; + mpInstance.triggerEvent = function (event, ...args) { + return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]) + }; +} + +Page = function (options = {}) { + const name = 'onLoad'; + const oldHook = options[name]; + if (!oldHook) { + options[name] = function () { + initTriggerEvent(this); + }; + } else { + options[name] = function (...args) { + initTriggerEvent(this); + return oldHook.apply(this, args) + }; + } + return WXPage(options) +}; + +const behavior = Behavior({ + created () { + initTriggerEvent(this); + } +}); + +Component = function (options = {}) { + (options.behaviors || (options.behaviors = [])).unshift(behavior); + return WXComponent(options) +}; + const MOCKS = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__']; function initMocks (vm) { @@ -280,7 +342,7 @@ function getData (vueOptions, context) { } Object.keys(methods).forEach(methodName => { - if (!hasOwn(data, methodName)) { + if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) { data[methodName] = methods[methodName]; } }); @@ -534,7 +596,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onUniNViewMessage' ]; function createApp (vm) { @@ -564,7 +627,7 @@ function createApp (vm) { }); const appOptions = { - onLaunch (args) { + onLaunch (args) { this.$vm = vm; diff --git a/packages/uni-app-plus/package.json b/packages/uni-app-plus/package.json index 60cbe2eeb..c25480d54 100644 --- a/packages/uni-app-plus/package.json +++ b/packages/uni-app-plus/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-app-plus", - "version": "0.0.207", + "version": "0.0.209", "description": "uni-app app-plus", "main": "dist/index.js", "scripts": { diff --git a/packages/uni-mp-weixin/dist/index.js b/packages/uni-mp-weixin/dist/index.js index e50cc4863..ce08e55da 100644 --- a/packages/uni-mp-weixin/dist/index.js +++ b/packages/uni-mp-weixin/dist/index.js @@ -19,7 +19,26 @@ function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } -function noop () {} +function noop () {} + +/** + * Create a cached version of a pure function. + */ +function cached (fn) { + const cache = Object.create(null); + return function cachedFn (str) { + const hit = cache[str]; + return hit || (cache[str] = fn(str)) + } +} + +/** + * Camelize a hyphen-delimited string. + */ +const camelizeRE = /-(\w)/g; +const camelize = cached((str) => { + return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') +}); const SYNC_API_RE = /requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$/; @@ -268,6 +287,49 @@ var api = /*#__PURE__*/Object.freeze({ }); +const WXPage = Page; +const WXComponent = Component; + +const customizeRE = /:/g; + +const customize = cached((str) => { + return camelize(str.replace(customizeRE, '-')) +}); + +function initTriggerEvent (mpInstance) { + const oldTriggerEvent = mpInstance.triggerEvent; + mpInstance.triggerEvent = function (event, ...args) { + return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]) + }; +} + +Page = function (options = {}) { + const name = 'onLoad'; + const oldHook = options[name]; + if (!oldHook) { + options[name] = function () { + initTriggerEvent(this); + }; + } else { + options[name] = function (...args) { + initTriggerEvent(this); + return oldHook.apply(this, args) + }; + } + return WXPage(options) +}; + +const behavior = Behavior({ + created () { + initTriggerEvent(this); + } +}); + +Component = function (options = {}) { + (options.behaviors || (options.behaviors = [])).unshift(behavior); + return WXComponent(options) +}; + const MOCKS = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__']; function initMocks (vm) { @@ -307,7 +369,7 @@ function getData (vueOptions, context) { } Object.keys(methods).forEach(methodName => { - if (!hasOwn(data, methodName)) { + if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) { data[methodName] = methods[methodName]; } }); @@ -561,7 +623,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onUniNViewMessage' ]; function createApp (vm) { @@ -591,7 +654,7 @@ function createApp (vm) { }); const appOptions = { - onLaunch (args) { + onLaunch (args) { { if (!wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断 console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上'); diff --git a/packages/uni-mp-weixin/package.json b/packages/uni-mp-weixin/package.json index 4b3c7a12a..e92913a1b 100644 --- a/packages/uni-mp-weixin/package.json +++ b/packages/uni-mp-weixin/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-mp-weixin", - "version": "0.0.927", + "version": "0.0.928", "description": "uni-app mp-weixin", "main": "dist/index.js", "scripts": { diff --git a/src/core/runtime/wrapper/create-app.js b/src/core/runtime/wrapper/create-app.js index f81792ea3..d3a8a21ce 100644 --- a/src/core/runtime/wrapper/create-app.js +++ b/src/core/runtime/wrapper/create-app.js @@ -1,3 +1,5 @@ +import 'uni-platform/runtime/index' + import Vue from 'vue' import { @@ -10,7 +12,8 @@ const hooks = [ 'onShow', 'onHide', 'onError', - 'onPageNotFound' + 'onPageNotFound', + 'onUniNViewMessage' ] export function createApp (vm) { @@ -40,7 +43,7 @@ export function createApp (vm) { }) const appOptions = { - onLaunch (args) { + onLaunch (args) { if (__PLATFORM__ === 'mp-weixin') { if (!wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断 console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上') diff --git a/src/core/runtime/wrapper/util.js b/src/core/runtime/wrapper/util.js index ccdc61dc9..5004db307 100644 --- a/src/core/runtime/wrapper/util.js +++ b/src/core/runtime/wrapper/util.js @@ -44,7 +44,7 @@ export function getData (vueOptions, context) { } Object.keys(methods).forEach(methodName => { - if (!hasOwn(data, methodName)) { + if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) { data[methodName] = methods[methodName] } }) diff --git a/src/platforms/app-plus/runtime/index.js b/src/platforms/app-plus/runtime/index.js new file mode 100644 index 000000000..5a843bc82 --- /dev/null +++ b/src/platforms/app-plus/runtime/index.js @@ -0,0 +1 @@ +import '../../mp-weixin/runtime/index' diff --git a/src/platforms/mp-alipay/runtime/index.js b/src/platforms/mp-alipay/runtime/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/src/platforms/mp-baidu/runtime/index.js b/src/platforms/mp-baidu/runtime/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/src/platforms/mp-toutiao/runtime/index.js b/src/platforms/mp-toutiao/runtime/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/src/platforms/mp-weixin/runtime/index.js b/src/platforms/mp-weixin/runtime/index.js new file mode 100644 index 000000000..f722360df --- /dev/null +++ b/src/platforms/mp-weixin/runtime/index.js @@ -0,0 +1,47 @@ +import { + cached, + camelize +} from 'uni-shared' + +const WXPage = Page +const WXComponent = Component + +const customizeRE = /:/g + +const customize = cached((str) => { + return camelize(str.replace(customizeRE, '-')) +}) + +function initTriggerEvent (mpInstance) { + const oldTriggerEvent = mpInstance.triggerEvent + mpInstance.triggerEvent = function (event, ...args) { + return oldTriggerEvent.apply(mpInstance, [customize(event), ...args]) + } +} + +Page = function (options = {}) { + const name = 'onLoad' + const oldHook = options[name] + if (!oldHook) { + options[name] = function () { + initTriggerEvent(this) + } + } else { + options[name] = function (...args) { + initTriggerEvent(this) + return oldHook.apply(this, args) + } + } + return WXPage(options) +} + +const behavior = Behavior({ + created () { + initTriggerEvent(this) + } +}) + +Component = function (options = {}) { + (options.behaviors || (options.behaviors = [])).unshift(behavior) + return WXComponent(options) +} diff --git a/src/shared/util.js b/src/shared/util.js index 78f25f81b..048f8e3b7 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -27,6 +27,25 @@ export function toRawType (val) { return _toString.call(val).slice(8, -1) } +/** + * Create a cached version of a pure function. + */ +export function cached (fn) { + const cache = Object.create(null) + return function cachedFn (str) { + const hit = cache[str] + return hit || (cache[str] = fn(str)) + } +} + +/** + * Camelize a hyphen-delimited string. + */ +const camelizeRE = /-(\w)/g +export const camelize = cached((str) => { + return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') +}) + export function setProperties (item, props, propsData) { props.forEach(function (name) { if (hasOwn(propsData, name)) { @@ -49,7 +68,7 @@ export function formatDateTime ({ } else { return date.getFullYear() + '-' + _completeValue(date.getMonth() + 1) + '-' + _completeValue(date.getDate()) } -} +} export function updateElementStyle (element, styles) { for (let attrName in styles) { -- GitLab