var api = require('../../utils/api'); Page({ data: { items: [], loading: false, error: '' }, onShow: function () { if (!api.user()) { this.setData({ items: [] }); my.redirectTo({ url: '/pages/splash/splash' }); return; } this.load(); }, onUnload: function () { this.closed = true; }, load: function () { var that = this; this.setData({ loading: true, error: '' }); api.request('reports').then(function (data) { if (that.closed) return; that.setData({ items: data.items.map(function (item) { var date = new Date(item.created_at); return { id: item.id, total: item.total == null ? '--' : item.total, skinAge: item.skinAge == null ? '--' : item.skinAge, date: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() }; }) }); }).catch(function (err) { if (!that.closed) that.setData({ error: err.message }); }) .then(function () { if (!that.closed) that.setData({ loading: false }); }); }, onOpen: function (e) { if (this.data.loading) return; var that = this; this.setData({ loading: true }); api.request('reports', { id: e.currentTarget.dataset.id }).then(function (data) { if (that.closed) return; getApp().globalData.photos = data.report.photos; getApp().globalData.payload = Object.assign({}, data.report.payload, { vm: data.report.vm, presentation: data.report.presentation }); my.navigateTo({ url: '/pages/report/report' }); }).catch(function (err) { my.showToast({ content: err.message, type: 'none' }); }) .then(function () { if (!that.closed) that.setData({ loading: false }); }); } });