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

feat(runtime): triggerEvent

上级 21ed9546
......@@ -60,6 +60,7 @@
"App": true,
"Page": true,
"Component": true,
"Behavior":true,
"getApp": true,
"getCurrentPages": true,
"plus": true,
......
......@@ -21,6 +21,25 @@ function hasOwn (obj, key) {
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 CONTEXT_API_RE = /^create|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) {
......
{
"name": "@dcloudio/uni-app-plus",
"version": "0.0.207",
"version": "0.0.209",
"description": "uni-app app-plus",
"main": "dist/index.js",
"scripts": {
......
......@@ -21,6 +21,25 @@ function hasOwn (obj, key) {
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 CONTEXT_API_RE = /^create|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) {
......
{
"name": "@dcloudio/uni-mp-weixin",
"version": "0.0.927",
"version": "0.0.928",
"description": "uni-app mp-weixin",
"main": "dist/index.js",
"scripts": {
......
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) {
......
......@@ -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]
}
})
......
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) {
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)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册