Skip to content

Commit

Permalink
feat(runtime): onShareAppMessage,props sync
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Mar 28, 2019
1 parent 6d451bf commit 69f4304
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
38 changes: 28 additions & 10 deletions packages/uni-mp-weixin/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function initMocks (vm) {
function initHooks (mpOptions, hooks) {
hooks.forEach(hook => {
mpOptions[hook] = function (args) {
this.$vm.__call_hook(hook, args);
return this.$vm.__call_hook(hook, args)
};
});
}
Expand Down Expand Up @@ -404,6 +404,14 @@ function processEventArgs (event, args = [], isCustom) {
const ONCE = '~';
const CUSTOM = '^';

function getTarget (obj, path) {
const parts = path.split('.');
if (parts.length === 1) {
return obj[parts[0]]
}
return getTarget(obj[parts[0]], parts.slice(1).join('.'))
}

function handleEvent (event) {
event = wrapper$1(event);

Expand All @@ -426,17 +434,27 @@ function handleEvent (event) {

if (eventsArray && eventType === type) {
eventsArray.forEach(eventArray => {
const handler = this.$vm[eventArray[0]];
if (!isFn(handler)) {
throw new Error(` _vm.${eventArray[0]} is not a function`)
}
if (isOnce) {
if (handler.once) {
return
const methodName = eventArray[0];
if (methodName === '$set') { // prop.sync
const args = eventArray[1];
if (args.length === 2) { // :title.sync="title"
this.$vm[args[0]] = event.detail[0];
} else if (args.length === 3) {
this.$vm.$set(getTarget(this.$vm, args[0]), args[1], event.detail[0]);
}
} else {
const handler = this.$vm[methodName];
if (!isFn(handler)) {
throw new Error(` _vm.${methodName} is not a function`)
}
if (isOnce) {
if (handler.once) {
return
}
handler.once = true;
}
handler.once = true;
handler.apply(this.$vm, processEventArgs(event, eventArray[1], isCustom));
}
handler.apply(this.$vm, processEventArgs(event, eventArray[1], isCustom));
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/uni-mp-weixin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dcloudio/uni-mp-weixin",
"version": "0.0.916",
"version": "0.0.918",
"description": "uni-app mp-weixin",
"main": "dist/index.js",
"scripts": {
Expand Down
38 changes: 28 additions & 10 deletions src/core/runtime/wrapper/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function initMocks (vm) {
export function initHooks (mpOptions, hooks) {
hooks.forEach(hook => {
mpOptions[hook] = function (args) {
this.$vm.__call_hook(hook, args)
return this.$vm.__call_hook(hook, args)
}
})
}
Expand Down Expand Up @@ -147,6 +147,14 @@ function processEventArgs (event, args = [], isCustom) {
const ONCE = '~'
const CUSTOM = '^'

function getTarget (obj, path) {
const parts = path.split('.')
if (parts.length === 1) {
return obj[parts[0]]
}
return getTarget(obj[parts[0]], parts.slice(1).join('.'))
}

export function handleEvent (event) {
event = wrapper(event)

Expand All @@ -169,17 +177,27 @@ export function handleEvent (event) {

if (eventsArray && eventType === type) {
eventsArray.forEach(eventArray => {
const handler = this.$vm[eventArray[0]]
if (!isFn(handler)) {
throw new Error(` _vm.${eventArray[0]} is not a function`)
}
if (isOnce) {
if (handler.once) {
return
const methodName = eventArray[0]
if (methodName === '$set') { // prop.sync
const args = eventArray[1]
if (args.length === 2) { // :title.sync="title"
this.$vm[args[0]] = event.detail[0]
} else if (args.length === 3) {
this.$vm.$set(getTarget(this.$vm, args[0]), args[1], event.detail[0])
}
} else {
const handler = this.$vm[methodName]
if (!isFn(handler)) {
throw new Error(` _vm.${methodName} is not a function`)
}
if (isOnce) {
if (handler.once) {
return
}
handler.once = true
}
handler.once = true
handler.apply(this.$vm, processEventArgs(event, eventArray[1], isCustom))
}
handler.apply(this.$vm, processEventArgs(event, eventArray[1], isCustom))
})
}
})
Expand Down

0 comments on commit 69f4304

Please sign in to comment.