From 6d2581cf26ce2d1e5d9c3cbed6fcf255a930d358 Mon Sep 17 00:00:00 2001 From: Jiayu Liu Date: Sat, 4 Aug 2018 01:51:04 +0800 Subject: [PATCH] update setData to include callback (#27832) --- types/weixin-app/index.d.ts | 10 ++++++++-- types/weixin-app/weixin-app-tests.ts | 20 ++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/types/weixin-app/index.d.ts b/types/weixin-app/index.d.ts index 85740f7481..100a00bc48 100644 --- a/types/weixin-app/index.d.ts +++ b/types/weixin-app/index.d.ts @@ -3218,9 +3218,14 @@ interface Component { */ data: T; /** - * 设置data并执行视图层渲染 + * 将数据从逻辑层发送到视图层,同时改变对应的 this.data 的值 + * 1. 直接修改 this.data 而不调用 this.setData 是无法改变页面的状态的,还会造成数据不一致。 + * 2. 单次设置的数据不能超过1024kB,请尽量避免一次设置过多的数据。 + * 3. 请不要把 data 中任何一项的 value 设为 undefined ,否则这一项将不被设置并可能遗留一些潜在问题 + * @param data object 以 key,value 的形式表示将 this.data 中的 key 对应的值改变成 value + * @param [callback] callback 是一个回调函数,在这次setData对界面渲染完毕后调用 */ - setData(data: object): void; + setData(data: { [key in keyof T]?: string | number | boolean | symbol | object | null | any[] }, callback?: () => any): void; /** * 检查组件是否具有 behavior * 检查时会递归检查被直接或间接引入的所有behavior @@ -3332,6 +3337,7 @@ interface PageOptions { */ onTabItemTap?: (item: any) => void; } + interface Page { /** * 强制更新 diff --git a/types/weixin-app/weixin-app-tests.ts b/types/weixin-app/weixin-app-tests.ts index 39943eba61..000fd36a3f 100644 --- a/types/weixin-app/weixin-app-tests.ts +++ b/types/weixin-app/weixin-app-tests.ts @@ -29,22 +29,31 @@ Component({ }, myProperty2: String // 简化的定义方式 }, - data: {}, // 私有数据,可用于模版渲染 + data: { + key: 'value', + anotherKey: 'value' + }, // 私有数据,可用于模版渲染 // 生命周期函数,可以为函数,或一个在methods段中定义的方法名 - attached() { }, + attached() { + this.setData({}, () => { }); + }, moved() { }, detached() { }, methods: { onMyButtonTap() { + // 更新属性和数据的方法与更新页面数据的方法类似 this.setData({ - // 更新属性和数据的方法与更新页面数据的方法类似 + key: 123 // note this is edge case where it cannot detect wrong types... }); }, _myPrivateMethod() { // 内部方法建议以下划线开头 // this.replaceDataOnPath(['A', 0, 'B'], 'myPrivateData'); // 这里将 data.A[0].B 设为 'myPrivateData' // this.applyDataUpdates(); + this.setData({ + anotherKey: 123 + }); }, _propertyChange(newVal: string, oldVal: string) { // @@ -71,8 +80,11 @@ Page({ data: { text: "This is page data." }, - onLoad: () => { + onLoad() { // Do some initialize when page load. + this.setData({}, () => { + // callback + }); }, onReady: () => { // Do something when page ready.