send notebook changes, 先添加父, 再添加子

This commit is contained in:
life
2015-03-13 11:00:40 +08:00
parent 7f11dd63ee
commit 3a78cca916
6 changed files with 129 additions and 47 deletions

2
node_modules/api.js generated vendored
View File

@@ -502,7 +502,7 @@ var Api = {
Content: note.Content, Content: note.Content,
IsMarkdown: note.IsMarkdown, IsMarkdown: note.IsMarkdown,
Tags: note.Tags, Tags: note.Tags,
IsBlog: note.IsBlog, IsBlog: false, // TODO 这里永远设为非blog note.IsBlog,
Files: note.Files, Files: note.Files,
FileDatas: note.FileDatas, FileDatas: note.FileDatas,
} }

2
node_modules/db.js generated vendored
View File

@@ -10,7 +10,7 @@ console.error(dbPath);
// test // test
if(dbPath.length < 6) { if(dbPath.length < 6) {
// var dbPath = '/Users/life/Library/Application Support/Leanote' + '/nedb'; var dbPath = '/Users/life/Library/Application Support/Leanote' + '/nedb';
} }
// console.log(dbPath); // console.log(dbPath);

100
node_modules/notebook.js generated vendored
View File

@@ -65,40 +65,48 @@ var Notebook = {
}); });
}, },
// 建立关联
_mapNotebooks: function(notebooks) {
var me = this;
// log(notebooks);
// 整理成层级关系, 并排序
// 1. 建立map
var notebooksMap = {};
for(var i in notebooks) {
var notebook = notebooks[i];
notebook.Subs = [];
notebooksMap[notebook.NotebookId] = notebook;
}
// 2. 追加到父下
var isolatedNotebooks = []; // 独立的, 没有父的, 第一级
for(var notebookId in notebooksMap) {
var notebook = notebooksMap[notebookId];
var parentNotebookId = notebook.ParentNotebookId;
if(parentNotebookId && notebooksMap[parentNotebookId]) {
notebooksMap[parentNotebookId].Subs.push(notebook);
} else {
isolatedNotebooks.push(notebook);
}
}
// 3. 排序
sortNotebooks(isolatedNotebooks);
// log(notebooks);
// log(notebooksMap['1'].Subs);
return isolatedNotebooks;
},
// 得到用户下所有的notebook // 得到用户下所有的notebook
// 排序好之后返回 // 排序好之后返回
getNotebooks: function(callback) { getNotebooks: function(callback) {
var me = this;
var userId = User.getCurActiveUserId(); var userId = User.getCurActiveUserId();
NB.find({UserId: userId, $or: [{LocalIsDelete : { $exists : false }}, {LocalIsDelete: false}] }, function(err, notebooks) { NB.find({UserId: userId, $or: [{LocalIsDelete : { $exists : false }}, {LocalIsDelete: false}] }, function(err, notebooks) {
if(err) { if(err) {
log(err); log(err);
return callback && callback(false); return callback && callback(false);
} }
// log(notebooks);
// 整理成层级关系, 并排序 callback && callback(me._mapNotebooks(notebooks));
// 1. 建立map
var notebooksMap = {};
for(var i in notebooks) {
var notebook = notebooks[i];
notebook.Subs = [];
notebooksMap[notebook.NotebookId] = notebook;
}
// 2. 追加到父下
var isolatedNotebooks = []; // 独立的, 没有父的, 第一级
for(var notebookId in notebooksMap) {
var notebook = notebooksMap[notebookId];
var parentNotebookId = notebook.ParentNotebookId;
if(parentNotebookId && notebooksMap[parentNotebookId]) {
notebooksMap[parentNotebookId].Subs.push(notebook);
} else {
isolatedNotebooks.push(notebook);
}
}
// 3. 排序
sortNotebooks(isolatedNotebooks);
// log(notebooks);
// log(notebooksMap['1'].Subs);
callback && callback(isolatedNotebooks);
}); });
}, },
@@ -127,7 +135,10 @@ var Notebook = {
}, },
updateNotebookTitle: function(notebookId, title, callback) { updateNotebookTitle: function(notebookId, title, callback) {
NB.update({NotebookId: notebookId}, {$set: {Title: title, IsDirty: true, UpdatedTime: new Date()}}, function(err, n) { NB.update({NotebookId: notebookId},
{$set:
{Title: title, IsDirty: true, UpdatedTime: new Date()}
}, function(err, n) {
callback(true); callback(true);
}); });
}, },
@@ -153,7 +164,11 @@ var Notebook = {
for(var i = 0; i < siblingNotebookIds.length; ++i) { for(var i = 0; i < siblingNotebookIds.length; ++i) {
var siblingNotebookId = siblingNotebookIds[i]; var siblingNotebookId = siblingNotebookIds[i];
console.log('siblingNotebookId: ' + siblingNotebookId); console.log('siblingNotebookId: ' + siblingNotebookId);
NB.update({NotebookId: siblingNotebookId}, {$set: {ParentNotebookId: parentNotebookId, Seq: i, IsDirty: true, UpdatedTime: new Date()}}); NB.update({NotebookId: siblingNotebookId},
{$set:
{ParentNotebookId: parentNotebookId, Seq: i, IsDirty: true, UpdatedTime: new Date()}
}
);
} }
}); });
}, },
@@ -346,14 +361,41 @@ var Notebook = {
}); });
}, },
// 深度优先一个列表
// 为了send changes时避免先send child
_deepTraversal: [],
_visited: {}, // 可以不要
deep: function(T) {
var me = this;
if(!T || !T.Subs || T.Subs.length == 0) {
return;
}
for(var i = 0; i < T.Subs.length; ++i) {
var node = T.Subs[i];
if(!me._visited[node.NotebookId]) { // 可以不要这个判断
me._visited[node.NotebookId] = true;
me._deepTraversal.push(T.Subs[i]);
// 递归
me.deep(T.Subs[i]);
}
}
},
// 获得用户修改的笔记本 // 获得用户修改的笔记本
getDirtyNotebooks: function(callback) { getDirtyNotebooks: function(callback) {
var me = this;
NB.find({UserId: User.getCurActiveUserId(), IsDirty: true}, function(err, notebooks) { NB.find({UserId: User.getCurActiveUserId(), IsDirty: true}, function(err, notebooks) {
if(err) { if(err) {
log(err); log(err);
return callback && callback(false); return callback && callback(false);
} else { } else {
callback(notebooks); var mapNotebooks = me._mapNotebooks(notebooks);
// 深度优先一个序列
me._deepTraversal = [];
me._visited = {};
me.deep({Subs: mapNotebooks});
// 返回之
callback(me._deepTraversal);
} }
}); });
}, },

24
node_modules/sync.js generated vendored
View File

@@ -670,22 +670,28 @@ var Sync = {
// 可能服务器上已删除, 此时应该要作为添加而不是更新 // 可能服务器上已删除, 此时应该要作为添加而不是更新
me._syncInfo.notebook.changeNeedAdds.push(notebook); me._syncInfo.notebook.changeNeedAdds.push(notebook);
} }
// me.checkNeedIncSyncAgain(newNotebook.Usn);
return cb();
} }
else { else {
// 更新 // 更新
// TODO 后端updateNotebook只要传Usn回来即可 // TODO 后端updateNotebook只要传Usn回来即可
Notebook.updateNotebookForceForSendChange(notebook.NotebookId, newNotebook); Notebook.updateNotebookForceForSendChange(notebook.NotebookId, newNotebook, function() {
if(notebook.LocalIsNew) {
if(notebook.LocalIsNew) {
// 没用 // 没用
me._syncInfo.notebook.changeAdds.push(newNotebook); me._syncInfo.notebook.changeAdds.push(newNotebook);
} else { } else {
// 没用 // 没用
me._syncInfo.notebook.updates.push(newNotebook); me._syncInfo.notebook.updates.push(newNotebook);
} }
// 这里才cb(), 因为先添加父, 再添加子
me.checkNeedIncSyncAgain(newNotebook.Usn);
cb();
});
} }
me.checkNeedIncSyncAgain(newNotebook.Usn);
cb();
}); });
}, function() { }, function() {
callback && callback(); callback && callback();

File diff suppressed because one or more lines are too long

46
test.js
View File

@@ -21,14 +21,14 @@ Api.addNotebook({
// Api.uploadImage(); // Api.uploadImage();
User.userId = '54bdc65599c37b0da9000002'; User.userId = '54bdc65599c37b0da9000002';
User.userId = '54d7620d99c37b030600002c'; User.userId = '54d7620d99c37b030600002c';
User.userId = '54f6e72899c37b6e20000044'; User.userId = '545b26ad38f4116d08000029';
// 54d7624205fcd105da00005 // 54d7624205fcd105da00005
// var reg = new RegExp(Evt.localUrl + '/api/file/getImage', 'g'); // var reg = new RegExp(Evt.localUrl + '/api/file/getImage', 'g');
// content = content.replace(reg, Evt.leanoteUrl + '/api/file/getImage'); // content = content.replace(reg, Evt.leanoteUrl + '/api/file/getImage');
Api.auth('leanote@leanote.com', 'myleanotelife121'); // Api.auth('leanote@leanote.com', 'myleanotelife121');
User.init(function() { User.init(function() {
@@ -37,15 +37,19 @@ User.init(function() {
console.log(note); console.log(note);
}); });
*/ */
Notebook.getDirtyNotebooks(function(notebooks) {
console.log(notebooks);
})
// Note.getNoteByServerNoteId('54f1a1f899c37b4faf000001', function(note) { // Note.getNoteByServerNoteId('54f1a1f899c37b4faf000001', function(note) {
// console.log(note); // console.log(note);
// }); // });
Note.getNotes('', function(ret) { // Note.getNotes('', function(ret) {
console.log(ret); // console.log(ret);
}); // });
}); });
@@ -115,4 +119,34 @@ while(s = reg.exec(a)) {
} }
*/ */
console.log(Common.goNowToDate('2014-01-06T18:29:48.802+08:00')); // console.log(Common.goNowToDate('2014-01-06T18:29:48.802+08:00'));
var A = {
_deepTraversal: [],
_visited: {},
deep: function(T) {
var me = this;
if(!T || !T.Subs || T.Subs.length == 0) {
return;
}
for(var i = 0; i < T.Subs.length; ++i) {
var node = T.Subs[i];
if(!me._visited[node.NotebookId]) {
me._visited[node.NotebookId] = true;
me._deepTraversal.push(T.Subs[i]);
me.deep(T.Subs[i]);
}
}
},
init: function() {
var nodes = [
{NotebookId: 1, Subs: [{NotebookId: 2, Subs: [{NotebookId: 9}]}, {NotebookId: 3, Subs: [{NotebookId: 4}, {NotebookId: 5}]}]},
{NotebookId: 6},
{NotebookId: 7}
]
this.deep({Subs: nodes});
console.log(this._deepTraversal);
}
};
// A.init();