历史记录回滚时强制保存当前为历史记录

This commit is contained in:
life
2015-12-04 18:23:17 +08:00
parent 628ce118fc
commit 512ec39a16
3 changed files with 38 additions and 13 deletions

11
node_modules/note.js generated vendored
View File

@@ -208,10 +208,11 @@ var Note = {
Histories []EachHistory `Histories` Histories []EachHistory `Histories`
} }
*/ */
addNoteHistory: function(noteId, content) { addNoteHistory: function(noteId, content, callback) {
var me = this; var me = this;
db.noteHistories.loadDB(function (ok) { db.noteHistories.loadDB(function (ok) {
if (!ok) { if (!ok) {
callback && callback(false);
return; return;
} }
// 先判断是否存在, 不存在则新建之 // 先判断是否存在, 不存在则新建之
@@ -219,7 +220,9 @@ var Note = {
var now = new Date(); var now = new Date();
// 新建之 // 新建之
if(!history) { if(!history) {
db.noteHistories.insert({_id: noteId, Histories: [{Content: content, "UpdatedTime": now}], "UpdatedTime": now}); db.noteHistories.insert({_id: noteId, Histories: [{Content: content, "UpdatedTime": now}], "UpdatedTime": now}, function () {
callback && callback(true);
});
} }
// 更新之 // 更新之
else { else {
@@ -235,7 +238,9 @@ var Note = {
} }
} }
db.noteHistories.update({_id: noteId}, {$set: {Histories: histories}}); db.noteHistories.update({_id: noteId}, {$set: {Histories: histories}}, function () {
callback && callback(true);
});
} }
}); });
}); });

View File

@@ -304,7 +304,6 @@ Note.curHasChanged = function(force) {
// 不会出现, 因为刚开始时readOnly=true, 且只有设置内容完成后才setCurNoteId // 不会出现, 因为刚开始时readOnly=true, 且只有设置内容完成后才setCurNoteId
// markdown编辑器还没准备好 // markdown编辑器还没准备好
throw new Error('markdown编辑器还没准备好'); throw new Error('markdown编辑器还没准备好');
alert(3);
} }
else { else {
content = contents; content = contents;
@@ -443,13 +442,32 @@ Note.getImgSrc = function(content) {
return ""; return "";
}; };
// revert 历史记录时强制添加到history中
Note.curChangedSaveItForRevertHistory = function(callback) {
var me = this;
me.curChangedSaveIt(true, function (ret) {
// 没有保存啊
if (!ret) {
var note = me.getCurNote();
var content = getEditorContent(note.IsMarkdown);
if (isArray(content)) {
content = content[0];
}
NoteService.addNoteHistory(me.curNoteId, content, callback);
}
else {
callback();
}
});
};
// 如果当前的改变了, 就保存它 // 如果当前的改变了, 就保存它
// 以后要定时调用 // 以后要定时调用
// force , 默认是true, 表强校验内容 // force , 默认是true, 表强校验内容
// 定时保存传false // 定时保存传false
Note.saveInProcess = {}; // noteId => bool, true表示该note正在保存到服务器, 服务器未响应 Note.saveInProcess = {}; // noteId => bool, true表示该note正在保存到服务器, 服务器未响应
Note.savePool = {}; // 保存池, 以后的保存先放在pool中, id => note Note.savePool = {}; // 保存池, 以后的保存先放在pool中, id => note
Note.curChangedSaveIt = function(force, callback) { Note.curChangedSaveIt= function(force, callback) {
var me = Note; var me = Note;
// 如果当前没有笔记, 不保存 // 如果当前没有笔记, 不保存
if(!me.curNoteId) { if(!me.curNoteId) {

View File

@@ -89,8 +89,9 @@ define(function() {
$tpl.find('.back').click(function() { $tpl.find('.back').click(function() {
if(confirm(getMsg("confirmBackup"))) { if(confirm(getMsg("confirmBackup"))) {
// 保存当前版本 // 保存当前版本
Note.curChangedSaveIt(true); // 如果没有变化, 肯定不会保存的
// Note.curChangedSaveIt(true);
Note.curChangedSaveItForRevertHistory(function () {
// 设置之 // 设置之
note = Note.cache[Note.curNoteId]; note = Note.cache[Note.curNoteId];
setEditorContent(me.list[me.curIndex].Content, note.IsMarkdown); setEditorContent(me.list[me.curIndex].Content, note.IsMarkdown);
@@ -98,6 +99,7 @@ define(function() {
$tpl.modal('hide'); $tpl.modal('hide');
// 保存 // 保存
Note.curChangedSaveIt(true); Note.curChangedSaveIt(true);
});
} }
}); });