当前位置:网站首页>Wechat applet - Global Monitoring of certain attribute changes of GlobalData, such as monitoring of network state switching
Wechat applet - Global Monitoring of certain attribute changes of GlobalData, such as monitoring of network state switching
2022-06-23 06:43:00 【Little sheep fighting bug】
Preface
Recently, there is a demand in the company's applet project , Product list page and product details page configuration video , Video in WiFi Automatically play in the environment , Not WiFi Pause playback in the environment .
One 、 Applet has access to network types API
wx.getNetworkType(Object object)
First, get whether the current network environment is WiFi
//app.js
App({
onLaunch: function() {
wx.getNetworkType({
success: function (res) {
if (res.networkType == 'wifi') {
that.globalData.isWifi = true
}
}
})
},
globalData: {
isWifi: false
}
})
On the product list page , Get to the current isWifi The attribute value
// goods_list.js
var app = getApp()
Page({
data: {
// Whether it is wifi Environmental Science
isWifi: false
},
onLoad: function(options) {
this.setData({
isWifi: app.globalData.isWifi
});
}
})
However, when the network is switched , The current network status obtained from the product list page will not change .
Two 、 Wechat applet provides monitoring for network state change events
wx.onNetworkStatusChange(function callback)
We can use this event to listen for network handover changes
wx.onNetworkStatusChange((res) => {
if (res.networkType == 'wifi') {
that.globalData.isWifi = true
} else {
that.globalData.isWifi = false
}
});
At this time , When the network state is switched, you can listen , however app.js in isWifi Attribute change , The value of the product list page does not change , What to do .
3、 ... and 、 Use Object.defineProperty() Listen for global changes to attributes
In retrospect vue Realize the principle of two-way data binding : Data hijacking combined with publishers - How subscriber mode works , adopt Object.defineProperty() To hijack the setter,getter, Publish messages to subscribers when data changes , Trigger corresponding listening callback .
// Monitor network state changes
setWatching: function (key,method) {
let obj = this.globalData;
// Add a prefix to generate hidden variables , Prevent dead circulation
let ori = obj[key]; //obj[key] This can't be put in Object.defineProperty in
if (ori){
// Handle declared variables , Binding processing
method(ori);
}
Object.defineProperty(obj, key, {
configurable: true, // Whether attributes can be deleted or redefined
enumerable: true, // Whether it can be enumerated , To put it simply, you are using Object.keys() when , Whether the key value can be obtained
set: function (value) {
this['_' + key] = value;
method(value); // Callback function when data changes , Realize synchronization function
},
get: function () {
// Call in other interfaces key When it's worth it , It will be implemented here .
if (typeof this['_' + key] == 'undefined'){
if(ori) {
// Here, variables and are hidden when reading data globalData The settings are different , So we have to do synchronization
this['_' + key] = ori;
return ori;
} else {
return undefined;
}
} else {
return this['_' + key];
}
}
})
}
Last modification , Product list page isWifi Method of attribute
// Monitor network state changes
app.setWatching('isWifi', (v)=>{
this.setData({
isWifi: app.globalData.isWifi
});
});
Now feel free to switch networks , You can listen on the product list page .
End .
边栏推荐
猜你喜欢

JS to create an array (all elements are objects)

Haas 506 2.0 Tutoriel de développement - bibliothèque de composants avancés - modem. SMS (ne prend en charge que les versions supérieures à 2,2)

解析创客教育中的个性化学习进度

Haas506 2.0 development tutorial - Advanced Component Library -modem SMS (only supports versions above 2.2)

Synchronous switching power supply reduces EMI layout dv/dt di/dt

原址 交换

WPF Command指令和INotifyPropertyChanged

Explain csma/cd, token bus and token ring clearly

mingw-w64、msys和ffmpeg的配置与编译

Index - MySQL
随机推荐
js 动态创建a href 循环下载文件只能下载10个或者固定数目的问题
Laravel log channel 分组配置
熟练利用切片操作
C语言学习总结
原址 交换
如何迁移virtualbox 的虚拟机到hype-v
MySQL ON DUPLICATE KEY 和 PgSQL ON CONFLICT(主键) 处理主键冲突
Open source to the world (Part 2): the power of open source from the evolution of database technology BDTC 2021
Softing dataFEED OPC Suite将西门子PLC数据存储到Oracle数据库中
The softing datafeed OPC suite stores Siemens PLC data in an Oracle Database
2020 smart power plant industry insight white paper
phpStudy设置301重定向
百度URL參數之LINK?URL參數加密解密研究(代碼實例)
JS to create an array (all elements are objects)
把CSMA/CD、Token Bus、Token Ring说清楚
直播带货这么火,如何在小程序中实现视频通话及直播互动功能?
20220621 Three Conjugates of Dual Quaternions
I heard you want to embed ppt on WordPress website?
Add IPAD control function into shairplay
Linked Storage