使用Promise模拟吧, 需要的话可以配合localStorage做本地的数据持久化,这样刷新页面也不会丢失数据。
一个简单的例子:
const CLASS_LIST = [
{
id: '1',
title: '1班',
ceiiling: 100,
open: true,
autoNotify: true,
autoPush: false,
masterList: [{ id: 1, perm: 1}],
masterRatio: '',
teacherRatio: '99'
},
{
id: '2',
title: '2班',
ceiiling: 100,
open: true,
autoNotify: true,
autoPush: false,
masterList: [],
masterRatio: '',
teacherRatio: '99'
}
]
export function getClassList () {
return new Promise((resolve, reject) => {
resolve(CLASS_LIST.map(item => ({ id: item.id, title: item.title })))
})
}
export function addClass (title) {
let id = 0
for (let item of CLASS_LIST) {
if (+item.id >= id) id = String(1 + (+item.id))
}
let classItem = {
id,
title,
ceiiling: 100,
open: true,
autoNotify: true,
autoPush: false,
masterList: []
}
CLASS_LIST.push(classItem)
return new Promise((resolve, reject) => {
resolve(classItem.id)
})
}