Skip to content

Commit

Permalink
fix(mp): set initial data for Page (#76)
Browse files Browse the repository at this point in the history
* chore: update deps

* fix(mp): set initial data for Page
  • Loading branch information
BuptStEve authored Apr 21, 2020
1 parent 79b7138 commit 8d9ae8f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 57 deletions.
40 changes: 20 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,30 @@
}
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/plugin-external-helpers": "^7.2.0",
"@babel/plugin-proposal-object-rest-spread": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@commitlint/cli": "^8.1.0",
"@commitlint/config-conventional": "^8.1.0",
"babel-eslint": "^10.0.2",
"babel-jest": "^24.8.0",
"eslint": "^6.1.0",
"eslint-config-standard": "^13.0.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^9.1.0",
"@babel/core": "^7.9.0",
"@babel/plugin-external-helpers": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.9.5",
"@babel/preset-env": "^7.9.5",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"babel-eslint": "^10.1.0",
"babel-jest": "^25.4.0",
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"gh-pages": "^2.1.1",
"husky": "^3.0.3",
"lerna": "^3.16.4",
"lint-staged": "^9.2.1",
"eslint-plugin-standard": "^4.0.1",
"gh-pages": "^2.2.0",
"husky": "^4.2.5",
"lerna": "^3.20.2",
"lint-staged": "^10.1.6",
"markdown-it-include": "^1.1.0",
"metro-memory-fs": "^0.55.0",
"rollup-plugin-babel": "^4.3.3",
"metro-memory-fs": "^0.59.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-eslint": "^7.0.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-replace": "^2.2.0",
"vuepress": "^1.0.3"
"vuepress": "^1.4.1"
}
}
29 changes: 15 additions & 14 deletions packages/tua-mp/examples/basic/utils/tua-mp.esm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* tua-mp version 0.8.3 */
/* tua-mp version 0.8.4 */
function _typeof(obj) {
"@babel/helpers - typeof";

if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
Expand Down Expand Up @@ -69,13 +71,13 @@ function _objectSpread2(target) {
var source = arguments[i] != null ? arguments[i] : {};

if (i % 2) {
ownKeys(source, true).forEach(function (key) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(source).forEach(function (key) {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
Expand Down Expand Up @@ -175,7 +177,7 @@ var logByType = function logByType(type) {
out[_key] = arguments[_key];
}

(_console = console)[type].apply(_console, ["[TUA-MP]:"].concat(out));
(_console = console)[type].apply(_console, ['[TUA-MP]:'].concat(out));
};
};

Expand Down Expand Up @@ -266,7 +268,7 @@ var setObjByPath = function setObjByPath(_ref) {
var parentStr = arr.slice(0, idx).reduce(function (acc, cur) {
return /\d/.test(cur) ? "".concat(acc, "[").concat(cur, "]") : "".concat(acc, ".").concat(cur);
}, 'this');
logger.error("Property \"".concat(cur, "\" is not found in \"").concat(parentStr, "\": ") + "Make sure that this property has initialized in the data option.");
logger.error("Property \"".concat(cur, "\" is not found in \"").concat(parentStr, "\": ") + 'Make sure that this property has initialized in the data option.');
}

if (idx === arr.length - 1) {
Expand Down Expand Up @@ -336,7 +338,7 @@ var checkReservedKeys = function checkReservedKeys(data, computed, methods) {
var reservedKeysInVm = getObjHasReservedKeys(data).concat(getObjHasReservedKeys(computed)).concat(getObjHasReservedKeys(methods)).join(', ');

if (reservedKeysInVm) {
throw Error("\u8BF7\u52FF\u5728 data\u3001computed\u3001methods " + "\u4E2D\u4F7F\u7528\u4E0B\u5217\u4FDD\u7559\u5B57:\n ".concat(reservedKeysInVm));
throw Error('请勿在 data、computed、methods ' + "\u4E2D\u4F7F\u7528\u4E0B\u5217\u4FDD\u7559\u5B57:\n ".concat(reservedKeysInVm));
}
};
/**
Expand Down Expand Up @@ -543,7 +545,7 @@ var hackSetData = function hackSetData(vm) {
});
};

var version = "0.8.3";
var version = "0.8.4";

/**
* 根据 vm 生成 key
Expand Down Expand Up @@ -586,9 +588,7 @@ var getWatchFnArrByVm = function getWatchFnArrByVm(vm) {
*/


var VmStatus =
/*#__PURE__*/
function () {
var VmStatus = /*#__PURE__*/function () {
function VmStatus() {
_classCallCheck(this, VmStatus);

Expand Down Expand Up @@ -849,9 +849,7 @@ var patchMethods2Array = function patchMethods2Array(_ref2) {
return arr;
};

var Dep =
/*#__PURE__*/
function () {
var Dep = /*#__PURE__*/function () {
function Dep() {
_classCallCheck(this, Dep);

Expand Down Expand Up @@ -1257,7 +1255,10 @@ var TuaPage = function TuaPage(_ref) {
computed = _ref$computed === void 0 ? {} : _ref$computed,
rest = _objectWithoutProperties(_ref, ["data", "watch", "methods", "computed"]);

return Page(_objectSpread2({}, rest, {}, methods, {
return Page(_objectSpread2({
// 设置初始值,不然自定义组件的 props 中将先接收到非值
data: isFn(rawData) ? rawData() : rawData
}, rest, {}, methods, {
onLoad: function onLoad() {
for (var _len = arguments.length, options = new Array(_len), _key = 0; _key < _len; _key++) {
options[_key] = arguments[_key];
Expand Down
29 changes: 15 additions & 14 deletions packages/tua-mp/examples/basic/utils/tua-mp.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* tua-mp version 0.8.3 */
/* tua-mp version 0.8.4 */
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function _typeof(obj) {
"@babel/helpers - typeof";

if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
Expand Down Expand Up @@ -73,13 +75,13 @@ function _objectSpread2(target) {
var source = arguments[i] != null ? arguments[i] : {};

if (i % 2) {
ownKeys(source, true).forEach(function (key) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(source).forEach(function (key) {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
Expand Down Expand Up @@ -179,7 +181,7 @@ var logByType = function logByType(type) {
out[_key] = arguments[_key];
}

(_console = console)[type].apply(_console, ["[TUA-MP]:"].concat(out));
(_console = console)[type].apply(_console, ['[TUA-MP]:'].concat(out));
};
};

Expand Down Expand Up @@ -270,7 +272,7 @@ var setObjByPath = function setObjByPath(_ref) {
var parentStr = arr.slice(0, idx).reduce(function (acc, cur) {
return /\d/.test(cur) ? "".concat(acc, "[").concat(cur, "]") : "".concat(acc, ".").concat(cur);
}, 'this');
logger.error("Property \"".concat(cur, "\" is not found in \"").concat(parentStr, "\": ") + "Make sure that this property has initialized in the data option.");
logger.error("Property \"".concat(cur, "\" is not found in \"").concat(parentStr, "\": ") + 'Make sure that this property has initialized in the data option.');
}

if (idx === arr.length - 1) {
Expand Down Expand Up @@ -340,7 +342,7 @@ var checkReservedKeys = function checkReservedKeys(data, computed, methods) {
var reservedKeysInVm = getObjHasReservedKeys(data).concat(getObjHasReservedKeys(computed)).concat(getObjHasReservedKeys(methods)).join(', ');

if (reservedKeysInVm) {
throw Error("\u8BF7\u52FF\u5728 data\u3001computed\u3001methods " + "\u4E2D\u4F7F\u7528\u4E0B\u5217\u4FDD\u7559\u5B57:\n ".concat(reservedKeysInVm));
throw Error('请勿在 data、computed、methods ' + "\u4E2D\u4F7F\u7528\u4E0B\u5217\u4FDD\u7559\u5B57:\n ".concat(reservedKeysInVm));
}
};
/**
Expand Down Expand Up @@ -547,7 +549,7 @@ var hackSetData = function hackSetData(vm) {
});
};

var version = "0.8.3";
var version = "0.8.4";

/**
* 根据 vm 生成 key
Expand Down Expand Up @@ -590,9 +592,7 @@ var getWatchFnArrByVm = function getWatchFnArrByVm(vm) {
*/


var VmStatus =
/*#__PURE__*/
function () {
var VmStatus = /*#__PURE__*/function () {
function VmStatus() {
_classCallCheck(this, VmStatus);

Expand Down Expand Up @@ -853,9 +853,7 @@ var patchMethods2Array = function patchMethods2Array(_ref2) {
return arr;
};

var Dep =
/*#__PURE__*/
function () {
var Dep = /*#__PURE__*/function () {
function Dep() {
_classCallCheck(this, Dep);

Expand Down Expand Up @@ -1261,7 +1259,10 @@ var TuaPage = function TuaPage(_ref) {
computed = _ref$computed === void 0 ? {} : _ref$computed,
rest = _objectWithoutProperties(_ref, ["data", "watch", "methods", "computed"]);

return Page(_objectSpread2({}, rest, {}, methods, {
return Page(_objectSpread2({
// 设置初始值,不然自定义组件的 props 中将先接收到非值
data: isFn(rawData) ? rawData() : rawData
}, rest, {}, methods, {
onLoad: function onLoad() {
for (var _len = arguments.length, options = new Array(_len), _key = 0; _key < _len; _key++) {
options[_key] = arguments[_key];
Expand Down
2 changes: 1 addition & 1 deletion packages/tua-mp/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tua-mp",
"version": "0.8.3",
"version": "0.8.4",
"description": "🖖一款类 Vue 的渐进式小程序框架(A progressive miniprogram framework for coding like Vue)",
"main": "examples/basic/utils/tua-mp.js",
"module": "examples/basic/utils/tua-mp.esm.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/tua-mp/src/TuaPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const TuaPage = ({
computed = {},
...rest
}) => Page({
// 设置初始值,不然自定义组件的 props 中将先接收到非值
data: isFn(rawData) ? rawData() : rawData,
...rest,
...methods,
onLoad (...options) {
Expand Down
4 changes: 2 additions & 2 deletions packages/tua-mp/src/utils/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const setObjByPath = ({ obj, path, val, isCheckDef = false }) => pathStr2

logger.error(
`Property "${cur}" is not found in "${parentStr}": ` +
`Make sure that this property has initialized in the data option.`
'Make sure that this property has initialized in the data option.'
)
}

Expand Down Expand Up @@ -156,7 +156,7 @@ export const checkReservedKeys = (data, computed, methods) => {

if (reservedKeysInVm) {
throw Error(
`请勿在 data、computed、methods ` +
'请勿在 data、computed、methods ' +
`中使用下列保留字:\n ${reservedKeysInVm}`
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tua-mp/src/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const logByType = (type) => (...out) => {
if (process.env.NODE_ENV === 'test') return

/* istanbul ignore next */
console[type](`[TUA-MP]:`, ...out)
console[type]('[TUA-MP]:', ...out)
}

export const logger = {
Expand Down
8 changes: 3 additions & 5 deletions packages/tua-mp/test/__tests__/TuaPage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,11 @@ describe('TuaPage', () => {
})

test('array item change should change computed', (done) => {
let n = 0

const vm = TuaPage({
data () {
return {
strArr: ['a', 'b'],
nestedArr: [{ num: n++, e: [{ v: { e: 'e' } }] }],
nestedArr: [{ num: 0, e: [{ v: { e: 'e' } }] }],
}
},
computed: {
Expand All @@ -357,8 +355,8 @@ describe('TuaPage', () => {
})

vm.strArr = ['c']
vm.nestedArr = [{ num: n++ }, { num: n++ }]
vm.nestedArr.push({ num: n++, s: { t: 't' } })
vm.nestedArr = [{ num: 1 }, { num: 2 }]
vm.nestedArr.push({ num: 3, s: { t: 't' } })

expect(vm.fromNestedArr.length).toBe(2)

Expand Down

0 comments on commit 8d9ae8f

Please sign in to comment.