Skip to content

Commit

Permalink
Merge pull request #508 from Meituan-Dianping/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
aOrz authored May 19, 2018
2 parents e8ae625 + ee07244 commit 29cee1a
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dist/vue.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4629,7 +4629,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
});

Vue$3.version = '2.4.1';
Vue$3.mpvueVersion = '1.0.8';
Vue$3.mpvueVersion = '1.0.11';

/* */

Expand Down
2 changes: 1 addition & 1 deletion dist/vue.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4627,7 +4627,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
});

Vue$3.version = '2.4.1';
Vue$3.mpvueVersion = '1.0.8';
Vue$3.mpvueVersion = '1.0.11';

/* */

Expand Down
2 changes: 1 addition & 1 deletion dist/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -4618,7 +4618,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
});

Vue$3.version = '2.4.1';
Vue$3.mpvueVersion = '1.0.8';
Vue$3.mpvueVersion = '1.0.11';

/* */

Expand Down
2 changes: 1 addition & 1 deletion dist/vue.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vue.runtime.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4625,7 +4625,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
});

Vue$3.version = '2.4.1';
Vue$3.mpvueVersion = '1.0.8';
Vue$3.mpvueVersion = '1.0.11';

/* */

Expand Down
2 changes: 1 addition & 1 deletion dist/vue.runtime.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4623,7 +4623,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
});

Vue$3.version = '2.4.1';
Vue$3.mpvueVersion = '1.0.8';
Vue$3.mpvueVersion = '1.0.11';

/* */

Expand Down
2 changes: 1 addition & 1 deletion dist/vue.runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4614,7 +4614,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
});

Vue$3.version = '2.4.1';
Vue$3.mpvueVersion = '1.0.8';
Vue$3.mpvueVersion = '1.0.11';

/* */

Expand Down
2 changes: 1 addition & 1 deletion dist/vue.runtime.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/mpvue-template-compiler/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4580,7 +4580,7 @@ var component = {
var mpcomid = ast.mpcomid;
var slots = ast.slots;
if (slotName) {
attrsMap['data'] = "{{...$root[$p], $root}}";
attrsMap['data'] = "{{...$root[$k], $root}}";
attrsMap['is'] = "{{" + slotName + "}}";
} else {
var slotsName = getSlotsName(slots);
Expand Down
2 changes: 1 addition & 1 deletion packages/mpvue-template-compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mpvue-template-compiler",
"version": "1.0.11",
"version": "1.0.12",
"description": "mpvue template compiler for Vue",
"main": "index.js",
"repository": {
Expand Down
49 changes: 48 additions & 1 deletion packages/mpvue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4144,7 +4144,7 @@ Object.defineProperty(Vue$3.prototype, '$ssrContext', {
});

Vue$3.version = '2.4.1';
Vue$3.mpvueVersion = '1.0.9';
Vue$3.mpvueVersion = '1.0.11';

/* globals renderer */

Expand Down Expand Up @@ -4974,6 +4974,48 @@ function getGlobalData (app, rootVueVM) {
}
}

/**
* 格式化 properties 属性,并给每个属性加上 observer 方法
*/
function normalizeProperties (vm) {
var properties = vm.$options.properties || {};
var res = {};
var val;
var loop = function ( key ) {
val = isPlainObject(properties[key])
? properties[key]
: { type: properties[key] };
res[key] = {
type: val.type,
value: val.value,
observer: function observer (newVal, oldVal) {
vm[key] = newVal; // 先修改值再触发原始的 observer,跟 watch 行为保持一致
if (typeof val.observer === 'function') {
val.observer.call(vm, newVal, oldVal);
}
}
};
};

for (var key in properties) loop( key );
return res
}

/**
* 把 properties 中的属性 proxy 到 vm 上
*/
function initMpProps (vm) {
var mpProps = vm._mpProps = {};
var keys = Object.keys(vm.$options.properties || {});
keys.forEach(function (key) {
if (!(key in vm)) {
proxy(vm, '_mpProps', key);
mpProps[key] = undefined; // for observe
}
});
observe(mpProps, true);
}

function initMP (mpType, next) {
var rootVueVM = this.$root;
if (!rootVueVM.$mp) {
Expand Down Expand Up @@ -5041,7 +5083,11 @@ function initMP (mpType, next) {
}
});
} else if (mpType === 'component') {
initMpProps(rootVueVM);

global.Component({
// 小程序原生的组件属性
properties: normalizeProperties(rootVueVM),
// 页面的初始数据
data: {
$root: {}
Expand Down Expand Up @@ -5192,6 +5238,7 @@ function getVmData (vm) {
var dataKeys = [].concat(
Object.keys(vm._data || {}),
Object.keys(vm._props || {}),
Object.keys(vm._mpProps || {}),
Object.keys(vm._computedWatchers || {})
);
return dataKeys.reduce(function (res, key) {
Expand Down
2 changes: 1 addition & 1 deletion packages/mpvue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mpvue",
"version": "1.0.11",
"version": "1.0.12",
"description": "Vue Runtime for mini program",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/mp/compiler/codegen/convert/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
convertComponent (ast, components, slotName) {
const { attrsMap, tag, mpcomid, slots } = ast
if (slotName) {
attrsMap['data'] = `{{...$root[$p], $root}}`
attrsMap['data'] = `{{...$root[$k], $root}}`
attrsMap['is'] = `{{${slotName}}}`
} else {
const slotsName = getSlotsName(slots)
Expand Down
49 changes: 48 additions & 1 deletion src/platforms/mp/runtime/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { handleError } from '../../../core/util/index'
import { isPlainObject } from 'shared/util'
import { handleError } from 'core/util/index'
import { observe } from 'core/observer/index'
import { proxy } from 'core/instance/state'

export function callHook (vm, hook, params) {
let handlers = vm.$options[hook]
Expand Down Expand Up @@ -37,6 +40,46 @@ function getGlobalData (app, rootVueVM) {
}
}

/**
* 格式化 properties 属性,并给每个属性加上 observer 方法
*/
function normalizeProperties (vm) {
const properties = vm.$options.properties || {}
const res = {}
let val
for (const key in properties) {
val = isPlainObject(properties[key])
? properties[key]
: { type: properties[key] }
res[key] = {
type: val.type,
value: val.value,
observer (newVal, oldVal) {
vm[key] = newVal // 先修改值再触发原始的 observer,跟 watch 行为保持一致
if (typeof val.observer === 'function') {
val.observer.call(vm, newVal, oldVal)
}
}
}
}
return res
}

/**
* 把 properties 中的属性 proxy 到 vm 上
*/
function initMpProps (vm) {
const mpProps = vm._mpProps = {}
const keys = Object.keys(vm.$options.properties || {})
keys.forEach(key => {
if (!(key in vm)) {
proxy(vm, '_mpProps', key)
mpProps[key] = undefined // for observe
}
})
observe(mpProps, true)
}

export function initMP (mpType, next) {
const rootVueVM = this.$root
if (!rootVueVM.$mp) {
Expand Down Expand Up @@ -100,7 +143,11 @@ export function initMP (mpType, next) {
}
})
} else if (mpType === 'component') {
initMpProps(rootVueVM)

global.Component({
// 小程序原生的组件属性
properties: normalizeProperties(rootVueVM),
// 页面的初始数据
data: {
$root: {}
Expand Down
1 change: 1 addition & 0 deletions src/platforms/mp/runtime/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function getVmData (vm) {
const dataKeys = [].concat(
Object.keys(vm._data || {}),
Object.keys(vm._props || {}),
Object.keys(vm._mpProps || {}),
Object.keys(vm._computedWatchers || {})
)
return dataKeys.reduce((res, key) => {
Expand Down
4 changes: 2 additions & 2 deletions test/mp/compiler/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ describe('slot', () => {
it('插槽', () => {
assertCodegen(
`<div><slot>test</slot></div>`,
`<template name="a"><view class="_div testModuleId"><template name="default">test</template><template data="{{...$root[$p], $root}}" is="{{$slotdefault || 'default'}}"></template></view></template>`,
`<template name="a"><view class="_div testModuleId"><template name="default">test</template><template data="{{...$root[$k], $root}}" is="{{$slotdefault || 'default'}}"></template></view></template>`,
{
name: 'a',
moduleId: 'testModuleId'
Expand All @@ -539,7 +539,7 @@ describe('slot', () => {
it('使用', () => {
assertCodegen(
`<div><slot name="w">test</slot></div>`,
`<template name="a"><view class="_div"><template name="w">test</template><template data="{{...$root[$p], $root}}" is="{{$slotw || 'w'}}"></template></view></template>`,
`<template name="a"><view class="_div"><template name="w">test</template><template data="{{...$root[$k], $root}}" is="{{$slotw || 'w'}}"></template></view></template>`,
{
name: 'a'
}
Expand Down

0 comments on commit 29cee1a

Please sign in to comment.