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

feat(runtime): triggerEvent

上级 21ed9546
......@@ -59,7 +59,8 @@
"globals": {
"App": true,
"Page": true,
"Component": true,
"Component": true,
"Behavior":true,
"getApp": true,
"getCurrentPages": true,
"plus": true,
......
......@@ -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;
......
{
"name": "@dcloudio/uni-app-plus",
"version": "0.0.207",
"version": "0.0.209",
"description": "uni-app app-plus",
"main": "dist/index.js",
"scripts": {
......
......@@ -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`以上');
......
{
"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) {
......@@ -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`以上')
......
......@@ -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)) {
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册