Skip to content

Commit

Permalink
日记作本地缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
小闹钟 committed Oct 14, 2016
1 parent 08a86c7 commit c84385c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
// app.js

const config = require('config');

App({

onLaunch: function () {
this.getDiaryList();
},

getUserInfo:function(cb){
},

getDiaryList() {
var that = this;

wx.getStorage({
key: config.storage.diaryListKey,
success: (res) => {
this.globalData.diaryList = res.data;
}
})
},

getDeviceInfo: function(callback) {
var that = this;

Expand All @@ -25,6 +39,7 @@ App({

globalData: {
deviceInfo: null,
diaryList: null,
}

})
6 changes: 6 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ module.exports = {
input: {
charWidth: 14, // 单个字符的宽度,in rpx
},

/** 本地存储 **/
// TODO 数据通过API全部存储于服务端
storage: {
diaryListKey: 'bearDiaryList',
}
};
18 changes: 17 additions & 1 deletion pages/new/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const IMAGE = 'IMAGE'
const mediaActionSheetItems = ['拍照', '选择照片', '选择视频'];
const mediaActionSheetBinds = ['chooseImage', 'chooseImage', 'chooseImage']

var app = getApp();

Page({

data: {
Expand Down Expand Up @@ -97,13 +99,27 @@ Page({
setDiary(diary) {
let layout = util.listToMatrix(diary.list, layoutColumnSize);
this.setData({diary: diary, layoutList: layout});
this.saveDiary(diary);
},

// 保存日记
// TODO sync to server
saveDiary(diary) {
const key = config.storage.diaryListKey;
let diaryList = app.globalData.diaryList || {};
diaryList[diary.meta.title] = diary;

wx.setStorage({key: key, data: diaryList});
},

// 页面初始化
onLoad: function(options) {
if (options) {
let title = options.title;
if (title) {this.setData({'diary.title': title});}
if (title) {this.setData({
'diary.meta.title': title,
'diary.meta.create_time': util.formatTime(new Date())
});}
}

this.init();
Expand Down

0 comments on commit c84385c

Please sign in to comment.