Skip to content

Commit

Permalink
autosaving
Browse files Browse the repository at this point in the history
  • Loading branch information
vijayrudraraju committed Jan 3, 2012
1 parent a5721f4 commit b756e5a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 40 deletions.
10 changes: 5 additions & 5 deletions _attachments/index.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>toiweb</title>
<title>werdmawb</title>

<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="viewport" content="user-scalable=no,width=device-width,initial-scale=1"/>
Expand All @@ -18,7 +18,7 @@
</head>

<body>
<div data-role="page" id="input" data-title="toiweb">
<div data-role="page" id="input" data-title="werdmawb">
<div id="account1">
</div>
<div data-role="content">
Expand All @@ -39,7 +39,7 @@
</div><!-- /footer -->
</div><!-- /page -->

<div data-role="page" id="query" data-title="toiweb">
<div data-role="page" id="query" data-title="werdmawb">
<div id="account2">
</div>
<div data-role="content">
Expand Down Expand Up @@ -68,7 +68,7 @@
</div><!-- /footer -->
</div><!-- /page -->

<div data-role="page" id="login" data-title="toiweb">
<div data-role="page" id="login" data-title="werdmawb">
<div data-role="header">
<h1>login</h1>
</div><!-- /header -->
Expand All @@ -85,7 +85,7 @@ <h1>login</h1>
</div><!-- /content -->
</div><!-- /page -->

<div data-role="page" id="signup" data-title="toiweb">
<div data-role="page" id="signup" data-title="werdmawb">
<div data-role="header">
<h1>signup</h1>
</div><!-- /header -->
Expand Down
11 changes: 6 additions & 5 deletions _attachments/js/main.js
Expand Up @@ -7,11 +7,11 @@ $(document).ready(function() {
console.log('$document.ready');
$.couch.app(function(app) {
$("#account1").evently("account", app);
$("#account2").evently("account", app);
//$("#account2").evently("account", app);
$("#profile1").evently("profile", app);
$("#profile2").evently("profile", app);
//$("#profile2").evently("profile", app);
$.evently.connect("#account1","#profile1", ["loggedIn","loggedOut"]);
$.evently.connect("#account2","#profile2", ["loggedIn","loggedOut"]);
//$.evently.connect("#account2","#profile2", ["loggedIn","loggedOut"]);
//$('#account').trigger('create');
});

Expand Down Expand Up @@ -109,8 +109,8 @@ $(document).ready(function() {
$(this).data('timeIndex',{});
$(this).data('assocIndex',{});

$(this).html('apple apartment application apart\nhello');
$(this).trigger('keyup');
//$(this).html('apple apartment application apart\nhello');
//$(this).trigger('keyup');
},
keyup: function(e) {
var newTime = $.now();
Expand Down Expand Up @@ -143,6 +143,7 @@ $(document).ready(function() {
// end word index

$(this).data('oldTime',newTime);
startAutoSaver();
}
});

Expand Down
27 changes: 27 additions & 0 deletions _attachments/js/util.js
Expand Up @@ -370,3 +370,30 @@ function calcLevenshteinDistance(firstWord,secondWord) {

return distMatrix[matrixWidth-1][matrixHeight-1];
}

function startAutoSaver() {
$('#profile1').data('autosave',true);
autoSaver();
}
function stopAutoSaver() {
$('#profile1').data('autosave',false);
}
function autoSaver() {
if ($('#profile1').data('autosave')) {
console.log('autosaving');
$('#profile1').html('<p>Saving...<p>');
var mainDb = $.couch.db('werdmawb');
var newDoc = $('#profile1').data('mainDoc');
newDoc['wordIndex'] = $('#textarea1').data('wordIndex');
newDoc['phraseIndex'] = $('#textarea1').data('phraseIndex');
mainDb.saveDoc(newDoc,
{
success: function(data) {
$('#profile1').html('<p>Saved!<p>');
stopAutoSaver();
var t = setTimeout('autoSaver()',10000);
}
}
);
}
}
92 changes: 62 additions & 30 deletions vendor/couchapp/evently/profile/loggedIn.js
Expand Up @@ -2,41 +2,73 @@ function(e, r) {
var userCtx = r.userCtx;
var widget = $(this);
// load the profile from the user doc
var db = $.couch.db(r.info.authentication_db);
var userDb = $.couch.db(r.info.authentication_db);
var mainDb = $.couch.db('werdmawb');
var userDocId = "org.couchdb.user:"+userCtx.name;
console.log('profile, loggedin');
db.openDoc(userDocId, {

console.log(userCtx.name+' profile, loggedin');

userDb.openDoc(userDocId, {
success : function(userDoc) {
console.log('open userdoc');
var profile = userDoc["couch.app.profile"];
if (profile) {
// we copy the name to the profile so it can be used later
// without publishing the entire userdoc (roles, pass, etc)
profile.name = userDoc.name;
$$(widget).profile = profile;
//widget.trigger("profileReady", [profile]);
} else {
//widget.trigger("noProfile", [userCtx]);
var newProfile = {};
// store the user profile on the user account document
$.couch.userDb(function(db) {
var userDocId = "org.couchdb.user:"+userDoc.name;
db.openDoc(userDocId, {
success : function(userDoc) {
console.log('open userdoc');
/*
userDoc["couch.app.profile"] = newProfile;
db.saveDoc(userDoc, {
success : function() {
newProfile.name = userDoc.name;
$$(widget).profile = newProfile;
}
});
*/
var profile = userDoc['couch.app.profile'];
if (profile['main']) {
// we copy the name to the profile so it can be used later
// without publishing the entire userdoc (roles, pass, etc)
profile.name = userDoc.name;
$('#profile1').data('profile', profile);
mainDb.openDoc(profile.main, {
success : function(mainDoc) {
$('#profile1').data('mainDoc',mainDoc);
$('#profile1').data('wordIndex',mainDoc.wordIndex);
$('#profile1').data('phraseIndex',mainDoc.phraseIndex);
$('#textarea1').data('wordIndex',mainDoc.wordIndex);
$('#textarea1').data('phraseIndex',mainDoc.phraseIndex);
startAutoSaver();
}
});
});
}
//widget.trigger("profileReady", [profile]);
} else {
//widget.trigger("noProfile", [userCtx]);
console.log('reinitializing user');
var newProfile = {};
var newId = '';
mainDb.saveDoc(
{
wordIndex:$('#textarea1').data('wordIndex'),
phraseIndex:$('#textarea1').data('phraseIndex')
},
{
success: function(data) {
console.log('maindoc saved');
console.log(data);
newId = data.id;
console.log('open maindoc');
newProfile.main = newId;
newProfile.name = userDoc.name;
userDoc['couch.app.profile'] = newProfile;
// store the user profile on the user account document
userDb.saveDoc(userDoc, {
success : function() {
console.log('save userdoc');
$('#profile1').data('profile', newProfile);
startAutoSaver();
}
});
mainDb.openDoc(newProfile.main, {
success : function(mainDoc) {
$('#profile1').data('mainDoc',mainDoc);
$('#profile1').data('wordIndex',mainDoc.wordIndex);
$('#profile1').data('phraseIndex',mainDoc.phraseIndex);
$('#textarea1').data('wordIndex',mainDoc.wordIndex);
$('#textarea1').data('phraseIndex',mainDoc.phraseIndex);
startAutoSaver();
}
});
}
}
);
}
}
});
}

0 comments on commit b756e5a

Please sign in to comment.