const { Class } = require("../../model/class"); const login = require("../../model/user"); const AV = require("../../lib/av-live-query-weapp-min.js"); const { jsonify } = require('../../utils/leancloudutils'); // pages/class/class.js const getDataForRender = class_ => ({ name: class_.get('name'), objectId: class_.get('objectId') }); Page({ /** 页面的初始数据data,用于与.wxlm文件绑定 classes: 课程列表 type: Array 元素class: 课程信息 class.objectId 唯一 class.objectId 课程唯一主键 class.name 课程名称 */ data: { classes: [], loading: true, }, presences: [], classes: [], add_class:function(event){ wx.navigateTo({ url: '../addclass/addclass', }) }, del_class: function(event){ class_id = event.currentTarget.dataset.class_id; class_name = event.currentTarget.dataset.class_name; presence = this.presences.filter(presence=>presence.get("class").id==class_id)[0]; if(presence==null){ console.warn("presence not in list, is it the teacher's classroom?") return; } pull_presences = this.pull_presences.bind(this); wx.showModal({ title: '退出学习', content: '退出学习会清零你该门课程的分数', success(res) { if (res.confirm) { wx.showLoading({ title: '请稍候', }) presence.destroy().then(() => { pull_presences(); wx.hideLoading(); wx.showToast({ title: '退出成功', icon: "success" }) }, () => { wx.hideLoading(); wx.showToast({ title: '操作失败', icon: "none" }) }).catch(error => console.error(error.message)); } } }); }, enter_class: function(event){ console.log(event); var class_id = event.currentTarget.dataset.class_id; var class_name = event.currentTarget.dataset.class_name; wx.navigateTo({ url: "../classroom/classroom?class_id="+class_id+"&"+"class_name="+class_name, }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, pull_presences: function(){ console.log("classes.pull_presences"); var query = new AV.Query("Presence"); query.equalTo("user", AV.User.current()); query.include("class"); return query.find().then((presences) => { this.presences = presences; console.log(presences); let classes = presences.map(presence => {return presence.get("class")}); console.log(classes); query = new AV.Query("Class_"); query.equalTo("teacher", AV.User.current()); query.include("teacher"); query.find().then(myclasses=>{ this.setData(jsonify({classes: classes.concat(myclasses), loading: false})); }) }).catch(console.error); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { console.log("classes.onShow()"); if(this.data.loading){ wx.showLoading({ title: '加载中', }) } this.pull_presences().then(wx.hideLoading); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { this.pull_presences(); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })