update setData to include callback (#27832)

This commit is contained in:
Jiayu Liu
2018-08-03 10:51:04 -07:00
committed by Sheetal Nandi
parent 9995645f5b
commit 6d2581cf26
2 changed files with 24 additions and 6 deletions
+8 -2
View File
@@ -3218,9 +3218,14 @@ interface Component<T> {
*/
data: T;
/**
* 设置data并执行视图层渲染
* 将数据从逻辑层发送到视图层,同时改变对应的 this.data 的值
* 1. 直接修改 this.data 而不调用 this.setData 是无法改变页面的状态的,还会造成数据不一致。
* 2. 单次设置的数据不能超过1024kB,请尽量避免一次设置过多的数据。
* 3. 请不要把 data 中任何一项的 value 设为 undefined ,否则这一项将不被设置并可能遗留一些潜在问题
* @param data object 以 keyvalue 的形式表示将 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 {
/**
* 强制更新
+16 -4
View File
@@ -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.