改变页面数据结构

smart_class
educoder_weapp 5 years ago
parent 35cb9a5ad4
commit 3c239f5595

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

@ -13,6 +13,12 @@ class Presence extends AV.Object{
get class(){
return this.get("class");
}
set isasking(value){
this.set("isasking", value);
}
get isasking(){
return this.get("isasking");
}
}
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) {
login();
login().then((user)=>{
console.log(user);
console.log(AV.User.current());
});
},
/**

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

@ -1,9 +1,9 @@
<!--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="grid-view">
<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>

Loading…
Cancel
Save