Skip to content

Instantly share code, notes, and snippets.

@chapel
Created January 12, 2011 23:19
Show Gist options
  • Save chapel/777098 to your computer and use it in GitHub Desktop.
Save chapel/777098 to your computer and use it in GitHub Desktop.
(function($) {
var app = $.sammy('#content', function() {
var db = $.couch.db('modern-forum');
//this.use('Couch');
this.helpers({
refreshThreads: function() {
db.view('modern-forum/threads', {
success: function(threads) {
threads.rows.forEach(function(thread) {
if (!$('li#' + thread.id).html()) {
$('#threads ul').append('<li id="' + thread.id + '"><a href="#/thread/' + thread.id + '"><div class="spacer">' + thread.value + '</div></a></li>');
}
});
}
});
}
})
this.get('#/', function (context) {
this.refreshThreads();
$('#content').empty();
$('#reply').addClass('hidden');
if ($('li.selectedThread').html()) {
$('li.selectedThread').removeClass('selectedThread');
}
});
this.post('#/reply', function (context) {
var fields = this.params, doc = {};
doc.content = fields['post'];
doc.datetime = new Date.now();
doc.thread_id = fields['thread_id'];
doc.type = 'post';
doc.user_id = 'chapel';
console.log(doc);
db.saveDoc(doc, {
success: function() {
this.redirect('#/thread/'+fields.thread_id);
},
error: function() {
alert('Error saving document.');
this.redirect('#/thread/'+fields.thread_id);
}
});
return false;
});
this.get('#/thread/:id', function (context) {
this.refreshThreads();
var id = this.params['id'];
$('#content').empty();
if ($('li.selectedThread').html()) {
$('li.selectedThread').removeClass('selectedThread');
}
$('li#' + id).addClass('selectedThread');
$('[name=thread_id]').val(id);
//console.log(id);
db.view('modern-forum/posts', {
key: id,
success: function(posts) {
var html;
posts.rows.forEach(function(post) {
post = post.value
html = '<div class="post"><a href="#/user/' + post.user_id + '" class="user"><img src="http://i.imgur.com/arExL.png" width="120" height="120" alt="" />' + post.user_id + '</a><div>' + post.content + '</div><div class="signature"></div>';
$('#content').append(html);
});
},
error: function(er) {
console.log(er);
}
});
});
});
$(function() {
$('#up button').click(function() {
$('#threads').scrollTo('-=200px', 300);
});
$('#down button').click(function() {
$('#threads').scrollTo('+=200px', 300);
});
app.run();
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment