完善了数据同步功能

smart_class
educoder_weapp 5 years ago
parent ac723f3c05
commit 35cb9a5ad4

@ -9,10 +9,10 @@ class Class extends AV.Object{
this.set("name", value);
}
get students(){
return this.get("students");
}
add_student(user){
this.addUnique("students", user.id);
}
del_student(user){
let students = this.get("students");
@ -20,23 +20,19 @@ class Class extends AV.Object{
this.remove(user.id);
}
}
static all() {
var query = new AV.Query("Class_");
return query.find();
}
enter() {
this.leave();
this._presence = new Presence({
user: AV.User.current(),
class: this
});
this._presence.save();
this._presence.save();
}
leave() {
if (this._presence != null) {
this._presence.destroy();
}
}
}
present_students() {
var query = new AV.Query("Presence");
//query.equalTo("class", this._class.id);

@ -1,55 +1,115 @@
// pages/classroom/classroom.js
const AV = require("../../lib/av-live-query-weapp-min")
const { jsonify } = require('../../utils/leancloudutils');
const bind = require("../../lib/live-query-binding")
Page({
/**
页面的初始数据
students: 该课程所有的学生
元素student
student.objectId 该学生的主键唯一
student.username 学生的用户名是唯一就像微信号一样
student.name 学生的姓名
*/
presence: null,
students: [],
present_students: [],
class: null,
class_id: null,
data: {
//以下数据的同步由该代码文件实现数据的呈现由classroom.wxml实现
present_students: [], // 出席的学生的用户名即上文提及的student.username
students: [], // 该课程所有的学生
unpresent_students: [], //没有出席的学生的用户名,不建议使用该数据,考虑弃用
//命名可能有点混乱......
present_students: [],
// 出席的学生的用户名即上文提及的student.username考虑弃用建议替换为presen_stu_ids
present_stu_ids: [], //出席的学生的主键objectId
students: [], // 该课程所有的学生, 元素类型见上文中的student
present_stus: [], //出席的学生元素类型与students中的一样
unpresent_students: [], //没有出席的学生的用户名,不建议使用该数据,弃用
class_name: "",//课程名称
class_id: "",//该课程的主键(编号)
},
fetch_present: function(){
pull_present: function(){
var query = new AV.Query("Presence");
//console.log(this.class);
query.equalTo("class", this.class);
query.include("user");
set_present = this.set_present.bind(this);
return AV.Promise.all([query.find().then(set_present), query.subscribe()]).then(([presents, subscription])=>{
this.subscription = subscription;
if(this.presenceUnbind) this.unbind();
this.presenceUnbind = bind(subscription, presents, set_present);
}).catch(error=> console.error(error.message));
},
set_present: function(presents){
//console.log("set_present");
//console.log(presents);
var student_ids = presents.map((present)=>{
user = present.get("user");
//console.log(user);
//console.log(typeof user);
return user.id;
//return user.get("username");
});
//console.log("student_ids:"+student_ids.toString());
//console.log("students:"+this.students);
present_stus = this.students.filter(student=> student_ids.indexOf(student.id)>-1)
//console.log(present_stus);
present_stu_usernames = present_stus.map(stu => stu.get("username"));
present_stu_ids = present_stus.map(stu=>stu.id)
this.setData({
present_stu_ids: present_stu_ids,
present_students: present_stu_usernames});
this.setData(jsonify({present_stus}));
return presents;
},
fetch_class: function(class_id){
this.class = AV.Object.createWithoutData("Class_", class_id);
return this.class.fetch().then((class_) => {
//console.log(this.class);
this.setData({
class_name: this.class.get("name"),
class_id: this.class.get("objectId")
});
}).catch(error=>console.error(error.message))
},
fetch_students: function(){
//console.log("fetch_students");
var query = new AV.Query("StudentClassMap");
query.equalTo("class", this.class);
query.include("user");
//console.log("fetch_students2")
return query.find();
},
set_students: function(studentClassMaps){
//console.log("set_students");
var students = []
studentClassMaps.forEach((scm, idx, a)=>{
console.log(a);
students.push(scm.get("user"));
});
this.students = students;
console.log("this.students");
console.log(students);
this.setData(jsonify((students)));
return students;
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.class = AV.Object.createWithoutData("Class_", options.class_id);
this.class.fetch().then((class_)=>{
console.log(this.class);
this.setData(this.class.get("name"));
})
this.class_id = options.class_id;
//this.class = AV.Object.createWithoutData("Class_", this.class_id);
this.fetch_class.bind(this)(this.class_id).then(this.fetch_students).then(this.set_students.bind(this)).then(this.pull_present.bind(this));
console.log("onLoad");
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
this.fetch_present().then((presents)=>{
console.log({students: presents.map((present)=>{return present.get("user")})});
this.setData(jsonify({students: presents.map((present)=>{return present.get("user")})}));
})
console.log("onReady");
},
@ -58,6 +118,7 @@ Page({
*/
onShow: function () {
console.log("onShow");
console.log("enter class when onShow");
this.class.enter();
},
@ -75,13 +136,14 @@ Page({
onUnload: function () {
this.class.leave();
console.log("onUnload");
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
this.pull_present.bind(this)();
},
/**

@ -1,3 +1,4 @@
{
"usingComponents": {}
"usingComponents": {},
"enablePullDownRefresh": true
}
Loading…
Cancel
Save