改变页面数据结构

smart_class
educoder_weapp 5 years ago
parent 35cb9a5ad4
commit 3c239f5595

@ -22,6 +22,8 @@ class Class extends AV.Object{
} }
enter() { enter() {
this.leave(); this.leave();
console.log("class.enter");
console.log(AV.User.current());
this._presence = new Presence({ this._presence = new Presence({
user: AV.User.current(), user: AV.User.current(),
class: this class: this

@ -13,6 +13,12 @@ class Presence extends AV.Object{
get class(){ get class(){
return this.get("class"); return this.get("class");
} }
set isasking(value){
this.set("isasking", value);
}
get isasking(){
return this.get("isasking");
}
} }
AV.Object.register(Presence, "Presence"); AV.Object.register(Presence, "Presence");

@ -0,0 +1,17 @@
const AV = require("../..//lib/av-live-query-weapp-min")
class StudentClassMap extends AV.Object{
get class(){
return this.get("class");
}
set class(value){
this.set("class", value);
}
get user(){
return this.get("user");
}
set user(value){
this.set("user", value);
}
}

@ -37,7 +37,10 @@ Page({
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
*/ */
onLoad: function (options) { onLoad: function (options) {
login(); login().then((user)=>{
console.log(user);
console.log(AV.User.current());
});
}, },
/** /**

@ -11,6 +11,8 @@ Page({
student.objectId 该学生的主键唯一 student.objectId 该学生的主键唯一
student.username 学生的用户名是唯一就像微信号一样 student.username 学生的用户名是唯一就像微信号一样
student.name 学生的姓名 student.name 学生的姓名
student.present 是否出席
student.isasking :0:没有提问,1:提问,2:请回答
*/ */
presence: null, presence: null,
students: [], students: [],
@ -18,15 +20,14 @@ Page({
class_id: null, class_id: null,
data: { data: {
//以下数据的同步由该代码文件实现数据的呈现由classroom.wxml实现 //以下数据的同步由该代码文件实现数据的呈现由classroom.wxml实现
//命名可能有点混乱......
present_students: [],
// 出席的学生的用户名即上文提及的student.username考虑弃用建议替换为presen_stu_ids
present_stu_ids: [], //出席的学生的主键objectId
students: [], // 该课程所有的学生, 元素类型见上文中的student students: [], // 该课程所有的学生, 元素类型见上文中的student
present_stus: [], //出席的学生元素类型与students中的一样 current_user: null, //目前登陆的用户,类型为对象属性有objectIdusername, name
unpresent_students: [], //没有出席的学生的用户名,不建议使用该数据,弃用 teacher: null,
class_name: "",//课程名称 //该课程教员,类型为对象
class_id: "",//该课程的主键(编号) //请在进入界面时判断用户是否为教员以给与权限或更改界面
//属性如下objectId, username, name
class_name: "", //课程名称
class_id: "", //该课程的主键(编号)
}, },
pull_present: function(){ pull_present: function(){
@ -36,31 +37,32 @@ Page({
query.include("user"); query.include("user");
set_present = this.set_present.bind(this); set_present = this.set_present.bind(this);
return AV.Promise.all([query.find().then(set_present), query.subscribe()]).then(([presents, subscription])=>{ return AV.Promise.all([query.find().then(set_present), query.subscribe()]).then(([presents, subscription])=>{
this.subscription = subscription; this.PresentSubscription = subscription;
if(this.presenceUnbind) this.unbind(); if(this.presentUnbind) this.presentUnbind();
this.presenceUnbind = bind(subscription, presents, set_present); this.presentUnbind = bind(subscription, presents, set_present);
}).catch(error=> console.error(error.message)); }).catch(error=> console.error(error.message));
}, },
set_present: function(presents){ set_present: function(presents){
//console.log("set_present"); //console.log("set_present");
//console.log(presents); //console.log(presents);
var student_ids = presents.map((present)=>{ var is_asking={};
var present_stu_ids = presents.map((present)=>{
user = present.get("user"); user = present.get("user");
//console.log(user); is_asking[user.id]=present.get("isasking");
//console.log(typeof user);
return user.id; return user.id;
//return user.get("username");
}); });
//console.log("student_ids:"+student_ids.toString()); students = this.students.map((student)=>{
//console.log("students:"+this.students); if(present_stu_ids.indexOf(student.id)>-1){
present_stus = this.students.filter(student=> student_ids.indexOf(student.id)>-1) student.set("ispresent",true);
//console.log(present_stus); student.set("isasking", is_asking[student.id]);
present_stu_usernames = present_stus.map(stu => stu.get("username")); }else{
present_stu_ids = present_stus.map(stu=>stu.id) student.set("ispresent",false);
this.setData({ student.set("isasking", 0);
present_stu_ids: present_stu_ids, }
present_students: present_stu_usernames});
this.setData(jsonify({present_stus})); return student;
})
this.setData(jsonify({students}));
return presents; return presents;
}, },
@ -119,6 +121,7 @@ Page({
onShow: function () { onShow: function () {
console.log("onShow"); console.log("onShow");
console.log("enter class when onShow"); console.log("enter class when onShow");
console.log(AV.User.current());
this.class.enter(); this.class.enter();
}, },
@ -136,7 +139,8 @@ Page({
onUnload: function () { onUnload: function () {
this.class.leave(); this.class.leave();
console.log("onUnload"); console.log("onUnload");
this.PresentSubscription.unsubscribe();
this.presentUnbind();
}, },
/** /**

@ -1,9 +1,9 @@
<!--pages/classroom/classroom.wxml--> <!--pages/classroom/classroom.wxml-->
<text>pages/classroom/classroom.wxml</text> <text>pages/classroom/classroom.wxml{{class_name}}{{class_id}}</text>
<view class="students"> <view class="students">
<view class="grid-view"> <view class="grid-view">
<view class="grid-cell" wx:for="{{students}}" wx:for-item="student" wx:key="objectId"> <view class="grid-cell" wx:for="{{students}}" wx:for-item="student" wx:key="objectId">
<text>{{student.username}}</text> <text>{{student.username}} {{student.ispresent}} {{student.isasking}}</text>
</view> </view>
</view> </view>
</view> </view>

Loading…
Cancel
Save