From aa076aaa2dd250aa02218393f23613a0f6191210 Mon Sep 17 00:00:00 2001 From: life Date: Sun, 1 Feb 2015 23:08:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=9D=91=E4=B8=80=E4=B8=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- node_modules/common.js | 8 ++++ node_modules/note.js | 91 ++++++++++++++++++++++++++++++++---------- public/js/app/note.js | 2 +- test.js | 4 +- 4 files changed, 81 insertions(+), 24 deletions(-) diff --git a/node_modules/common.js b/node_modules/common.js index 6d5d2e19..1001a7e8 100644 --- a/node_modules/common.js +++ b/node_modules/common.js @@ -5,6 +5,14 @@ var ObjectId = require('objectid'); // var gui = require('nw.gui'); // console.log(gui.App); +// +process.on('uncaughtException', function (err) { + // 打印出错误 + console.log('~!!~ uncaughtException ~!!~'); + console.log(err); + // 打印出错误的调用栈方便调试 + console.log(err.stack); +}); function log(o) {console.log(o)} // log("<>>>>>>>>>>>>>>>>>>>>"); diff --git a/node_modules/note.js b/node_modules/note.js index 93725188..086871be 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -371,24 +371,7 @@ var Note = { }); }, - // 删除附件 - deleteAttachs: function(attachs) { - var me = this; - var fileBasePath = User.getCurUserAttachsPath(); - if(!attachs) { - return; - } - for(var i in attachs) { - var path = attachs[i].Path; - if(path && path.indexOf(fileBasePath) > 0) { - try { - fs.unlink(path); - } catch(e) { - console.log(e); - } - } - } - }, + // sync <- 时 // 更新笔记, 合并之, 内容要重新获取 @@ -879,10 +862,76 @@ var Note = { var me = this; console.log('updateAttach'); console.log(attachs); - Notes.update({NoteId: noteId}, {$set: {Attachs: attachs, IsDirty: true}}); - // File, 删除修改了的 - File.deleteNotExistsAttach(noteId, attachs); + // 删除修改了的 + me.deleteNotExistsAttach(noteId, attachs, function() { + // 一个坑!!!!!!!!!!!, js是引用的, needb并不会立即写到硬盘上, 在内存中是一个引用 + var t = []; + for(var i in attachs) { + t.push(attachs[i]); + } + Notes.update({NoteId: noteId}, {$set: {Attachs: t, IsDirty: true, UpdatedTime: new Date()}} ); + }); + }, + + // web端操作, 删除attach时, 删除不要的attach + deleteNotExistsAttach: function(noteId, attachs, callback) { + var me = this; + // console.log('--'); + me.getNote(noteId, function(note) { + if(!note) { + callback(); + return; + } + var everAttachs = note.Attachs || []; + var nowMap = {}; + for(var i in attachs) { + nowMap[attachs[i].FileId] = attachs[i]; + } + // console.log(note); + // console.log('end'); + // console.log(everAttachs.length); + // console.log(attachs.length); + // console.log(attachs == everAttachs); + var fileBasePath = User.getCurUserAttachsPath(); + for(var i in everAttachs) { + var attach = everAttachs[i]; + var path = attach.Path; + if(!nowMap[attach.FileId]) { // 如果不在, 则删除之 + // console.log(">>>>>>>>>"); + try { + // 删除源文件, 别删错了啊 + if(path.indexOf(fileBasePath) >= 0) { + fs.unlink(path); + } + } catch(e) { + console.log(e); + } + } + } + + // 一个坑!!!!!!!!!!! + callback(); + }); + }, + + // 删除附件, 在sync时 + deleteAttachs: function(attachs) { + var me = this; + var fileBasePath = User.getCurUserAttachsPath(); + if(!attachs) { + return; + } + for(var i in attachs) { + var path = attachs[i].Path; + if(path && path.indexOf(fileBasePath) > 0) { + try { + fs.unlink(path); + } catch(e) { + console.log(e); + } + } + } }, }; diff --git a/public/js/app/note.js b/public/js/app/note.js index c8e3d4fb..82d16e51 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -1725,7 +1725,7 @@ var Attach = { */ var html = ""; var attachNum = attachs.length; - console.log(attachs); + // console.log(attachs); for(var i = 0; i < attachNum; ++i) { var each = attachs[i]; var path = each.Path; diff --git a/test.js b/test.js index 4ad40942..350c0ff1 100755 --- a/test.js +++ b/test.js @@ -13,12 +13,12 @@ Notebook.addNotebook("3", "工作"); Notebook.addNotebook("4", "life2", "1"); Api.addNotebook({ - Title: "哈哈" + Title: "哈哈"54ce31fa99c37b1d5c00057e }, function() {}); */ // Api.uploadImage(); User.userId = '54bdc65599c37b0da9000002'; -Note.getNoteByServerNoteId('54ce29b399c37b1d5c000479', function(doc) { +Note.getNoteByServerNoteId('54ce32fc99c37b1d5c0005a4', function(doc) { console.log(doc); }); /*