提交 99335593 编写于 作者: fxy060608's avatar fxy060608

feat(runtime): triggerEvent

上级 21ed9546
...@@ -59,7 +59,8 @@ ...@@ -59,7 +59,8 @@
"globals": { "globals": {
"App": true, "App": true,
"Page": true, "Page": true,
"Component": true, "Component": true,
"Behavior":true,
"getApp": true, "getApp": true,
"getCurrentPages": true, "getCurrentPages": true,
"plus": true, "plus": true,
......
...@@ -19,7 +19,26 @@ function hasOwn (obj, key) { ...@@ -19,7 +19,26 @@ function hasOwn (obj, key) {
return hasOwnProperty.call(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$/; const SYNC_API_RE = /requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$/;
...@@ -241,6 +260,49 @@ var api = /*#__PURE__*/Object.freeze({ ...@@ -241,6 +260,49 @@ var api = /*#__PURE__*/Object.freeze({
requireNativePlugin: requireNativePlugin 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__']; const MOCKS = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__'];
function initMocks (vm) { function initMocks (vm) {
...@@ -280,7 +342,7 @@ function getData (vueOptions, context) { ...@@ -280,7 +342,7 @@ function getData (vueOptions, context) {
} }
Object.keys(methods).forEach(methodName => { Object.keys(methods).forEach(methodName => {
if (!hasOwn(data, methodName)) { if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) {
data[methodName] = methods[methodName]; data[methodName] = methods[methodName];
} }
}); });
...@@ -534,7 +596,8 @@ const hooks = [ ...@@ -534,7 +596,8 @@ const hooks = [
'onShow', 'onShow',
'onHide', 'onHide',
'onError', 'onError',
'onPageNotFound' 'onPageNotFound',
'onUniNViewMessage'
]; ];
function createApp (vm) { function createApp (vm) {
...@@ -564,7 +627,7 @@ function createApp (vm) { ...@@ -564,7 +627,7 @@ function createApp (vm) {
}); });
const appOptions = { const appOptions = {
onLaunch (args) { onLaunch (args) {
this.$vm = vm; this.$vm = vm;
......
{ {
"name": "@dcloudio/uni-app-plus", "name": "@dcloudio/uni-app-plus",
"version": "0.0.207", "version": "0.0.209",
"description": "uni-app app-plus", "description": "uni-app app-plus",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
......
...@@ -19,7 +19,26 @@ function hasOwn (obj, key) { ...@@ -19,7 +19,26 @@ function hasOwn (obj, key) {
return hasOwnProperty.call(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$/; const SYNC_API_RE = /requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$/;
...@@ -268,6 +287,49 @@ var api = /*#__PURE__*/Object.freeze({ ...@@ -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__']; const MOCKS = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__'];
function initMocks (vm) { function initMocks (vm) {
...@@ -307,7 +369,7 @@ function getData (vueOptions, context) { ...@@ -307,7 +369,7 @@ function getData (vueOptions, context) {
} }
Object.keys(methods).forEach(methodName => { Object.keys(methods).forEach(methodName => {
if (!hasOwn(data, methodName)) { if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) {
data[methodName] = methods[methodName]; data[methodName] = methods[methodName];
} }
}); });
...@@ -561,7 +623,8 @@ const hooks = [ ...@@ -561,7 +623,8 @@ const hooks = [
'onShow', 'onShow',
'onHide', 'onHide',
'onError', 'onError',
'onPageNotFound' 'onPageNotFound',
'onUniNViewMessage'
]; ];
function createApp (vm) { function createApp (vm) {
...@@ -591,7 +654,7 @@ function createApp (vm) { ...@@ -591,7 +654,7 @@ function createApp (vm) {
}); });
const appOptions = { const appOptions = {
onLaunch (args) { onLaunch (args) {
{ {
if (!wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断 if (!wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断
console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上'); console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上');
......
{ {
"name": "@dcloudio/uni-mp-weixin", "name": "@dcloudio/uni-mp-weixin",
"version": "0.0.927", "version": "0.0.928",
"description": "uni-app mp-weixin", "description": "uni-app mp-weixin",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
......
import 'uni-platform/runtime/index'
import Vue from 'vue' import Vue from 'vue'
import { import {
...@@ -10,7 +12,8 @@ const hooks = [ ...@@ -10,7 +12,8 @@ const hooks = [
'onShow', 'onShow',
'onHide', 'onHide',
'onError', 'onError',
'onPageNotFound' 'onPageNotFound',
'onUniNViewMessage'
] ]
export function createApp (vm) { export function createApp (vm) {
...@@ -40,7 +43,7 @@ export function createApp (vm) { ...@@ -40,7 +43,7 @@ export function createApp (vm) {
}) })
const appOptions = { const appOptions = {
onLaunch (args) { onLaunch (args) {
if (__PLATFORM__ === 'mp-weixin') { if (__PLATFORM__ === 'mp-weixin') {
if (!wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断 if (!wx.canIUse('nextTick')) { // 事实 上2.2.3 即可,简单使用 2.3.0 的 nextTick 判断
console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上') console.error('当前微信基础库版本过低,请将 微信开发者工具-详情-项目设置-调试基础库版本 更换为`2.3.0`以上')
......
...@@ -44,7 +44,7 @@ export function getData (vueOptions, context) { ...@@ -44,7 +44,7 @@ export function getData (vueOptions, context) {
} }
Object.keys(methods).forEach(methodName => { Object.keys(methods).forEach(methodName => {
if (!hasOwn(data, methodName)) { if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) {
data[methodName] = methods[methodName] data[methodName] = methods[methodName]
} }
}) })
......
import '../../mp-weixin/runtime/index'
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)
}
...@@ -27,6 +27,25 @@ export function toRawType (val) { ...@@ -27,6 +27,25 @@ export function toRawType (val) {
return _toString.call(val).slice(8, -1) 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) { export function setProperties (item, props, propsData) {
props.forEach(function (name) { props.forEach(function (name) {
if (hasOwn(propsData, name)) { if (hasOwn(propsData, name)) {
...@@ -49,7 +68,7 @@ export function formatDateTime ({ ...@@ -49,7 +68,7 @@ export function formatDateTime ({
} else { } else {
return date.getFullYear() + '-' + _completeValue(date.getMonth() + 1) + '-' + _completeValue(date.getDate()) return date.getFullYear() + '-' + _completeValue(date.getMonth() + 1) + '-' + _completeValue(date.getDate())
} }
} }
export function updateElementStyle (element, styles) { export function updateElementStyle (element, styles) {
for (let attrName in styles) { for (let attrName in styles) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册