From 3d1397a7831b0fab454c41bab986a7c55c5f738d Mon Sep 17 00:00:00 2001 From: Aiko Mastboom Date: Fri, 26 Apr 2013 14:36:28 +0200 Subject: [PATCH] update sharedoc when server does it's thing behind the scenes --- mongodata.js | 69 ++++++++++++++++++++++++++++++++++++++++++++--- public/index.html | 8 +++--- routes.js | 2 +- 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/mongodata.js b/mongodata.js index dafdfc0..c5ca330 100644 --- a/mongodata.js +++ b/mongodata.js @@ -1,7 +1,7 @@ var ObjectID = require('mongodb').ObjectID; -module.exports = function (db, config) { +module.exports = function (db, shareModel, config) { /* options: @@ -224,15 +224,73 @@ module.exports = function (db, config) { } }; config.debug && console.log('getMongoAttribute attribute_options', attribute_options); + function updateShareDocument(documentId, attribute_result, options) { + var ops = []; + shareModel.getSnapshot(documentId, function (err, doc) { + if (err) { + config.errors && console.log('ERR1 setMongoAttribute updateShareDocument shareModel.getSnapshot', documentId, err); + } else { + if (!doc.snapshot.hasOwnProperty(options.attribute)) { + ops.push({ + p: [options.attribute], + oi: { guid: attribute_result._id } + }); + } else if (!doc.snapshot[options.attribute].hasOwnProperty('guid')) { + ops.push({ + p: [options.attribute, 'guid'], + oi: attribute_result._id + }) + } else { + ops.push({ + p: [options.attribute, 'guid'], + od: doc.snapshot[options.attribute].guid, + oi: attribute_result._id + + }) + } + shareModel.applyOp(documentId, { op: ops, v: doc.v }, function (err, result) { + if (err) { + config.errors && console.log('ERR1 setMongoAttribute updateShareDocument shareModel.applyOp', documentId, err); + } + //config.debug && + console.log('1 shareModel.applyOp', documentId, ops, err, result); + }); + } + }); + } + + function createShareDocument(documentId, attribute_data) { + var ops = []; + shareModel.getSnapshot(documentId, function (err, doc) { + if (err) { + config.errors && console.log('ERR2 setMongoAttribute createShareDocument shareModel.getSnapshot', documentId, err); + } else { + ops.push({ + p: [], + oi: attribute_data + + }); + shareModel.applyOp(documentId, { op: ops, v: doc.v }, function (err, result) { + if (err) { + config.errors && console.log('ERR1 setMongoAttribute createShareDocument shareModel.applyOp', documentId, err); + } + //config.debug && + console.log('2 shareModel.applyOp', documentId, ops, err, result); + }); + } + }); + } + return getMongoContent(attribute_options, function attribute(err, attribute_result) { + var documentId = 'json:' + options.collection + ':' + result.name; if (attribute_result) { config.debug && console.log('getMongoAttribute found lost attribute, reconnect'); if (result[options.attribute]) { result[options.attribute].guid = attribute_result._id; - // TODO: make same change on sharedoc. + updateShareDocument(documentId, attribute_result, options) } else { result[options.attribute] = { guid: attribute_result._id }; - // TODO: make same change on sharedoc. + updateShareDocument(documentId, attribute_result, options) } return saveData(col, result, callback); @@ -246,13 +304,16 @@ module.exports = function (db, config) { if (options.operation) { attribute_data.version = options.operation.v; } + var attributeDocumentId = documentId + ':' + options.attribute; + createShareDocument(attributeDocumentId, attribute_data); return saveData(col, attribute_data, function saved(err, attribute_result) { if (err) { config.errors && console.log('ERR3 setMongoAttribute', err); } result[options.attribute] = { guid: attribute_result._id }; - // TODO: make same change on sharedoc. + + updateShareDocument(documentId, attribute_result, options); return saveData(col, result, callback); }) } diff --git a/public/index.html b/public/index.html index 9be24cb..ec2f4c0 100644 --- a/public/index.html +++ b/public/index.html @@ -214,8 +214,7 @@ function main_functions(app, viewModel, vm_config) { op.push({ p: [attribute, 'mode'], od: app.doc.main.snapshot[attribute].mode - - }) + }); } if (mode && mode != 'none') { debug && console.log('//insert', attribute, mode); @@ -232,6 +231,7 @@ function main_functions(app, viewModel, vm_config) { }, setDoc: function (attribute) { + //noinspection JSUnresolvedFunction editor.ace.setReadOnly(true); var mode = (app.doc.main.snapshot && app.doc.main.snapshot[attribute] @@ -256,7 +256,9 @@ function main_functions(app, viewModel, vm_config) { console.error(error); return; } + //noinspection JSUnresolvedFunction editor.doc.attach_ace(editor.ace); + //noinspection JSUnresolvedFunction editor.ace.setReadOnly(false); editor.ace.focus(); //To focus the ace editor }); @@ -357,7 +359,7 @@ function initViewModelMain(app, doc, vm_config) { }, function (err, result) { //viewModel._newAttribute(""); }); - app.fn.main.setDoc(newValue); + viewModel._goToAttribute(newValue); } }); diff --git a/routes.js b/routes.js index 1c9dbd1..40d1234 100644 --- a/routes.js +++ b/routes.js @@ -13,7 +13,7 @@ module.exports = function (app, db, config) { var server = ShareJS.server.attach(app, config.share); var model = app.model; - var mongoDataInstance = mongoData(db, config); + var mongoDataInstance = mongoData(db, model, config); var route; route = config.api.data + '/:collection/:guid/:attribute.:ext(css|less|js|html)'; app.get(route,