Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CRUD feature done!

  • Loading branch information...
commit f56562e637910e684ea3f32a38a023632147ff35 1 parent 9e0c90b
@yssk22 authored
View
2  apps/posts/_attachments/js/posts.js
@@ -7,6 +7,7 @@
$.fn.bindList = function(){
var target = this;
var link = target.find('.readmore');
+ console.log(link.prev().html());
link.bind('click', function(e){
var last = link.prev();
var path = link.attr('data-apipath');
@@ -15,6 +16,7 @@
startkey_docid: last.attr('data-docid'),
skip: 1
};
+
$.get(path, param, function(data){
link.before(data);
var newlast = link.prev();
View
17 apps/posts/_attachments/templates/admin/index.ejs
@@ -1,7 +1,20 @@
-<div class="posts">
+<% title(_("posts.admin.managePosts")) %>
+<div class="posts admin">
<h2><%= _("posts.admin.managePosts") %></h2>
<div class="main">
- aaa
+ <% if( posts.data.length > 0 ){ %>
+ <div class="list">
+ <%- partial('parts/posts', {object: posts.data, as: this}) %>
+ <div class="readmore" data-apipath="<%= url('/admin/-/') %>">
+ <%= _('posts.read_more') %>
+ </div>
+ </div>
+ <script type="text/javascript">
+ $(function(){ $('div.posts. div.main div.list').bindList(); });
+ </script>
+ <% }else{ %>
+ <p><%= _('posts.no_posts_found') %></p>
+ <% } %>
</div>
<aside class="sidebar">
<%- partial('parts/menu') %>
View
4 apps/posts/_attachments/templates/index.ejs
@@ -9,7 +9,9 @@
<%= _('posts.read_more') %>
</div>
</div>
- <script type="text/javascript">$(function(){ $('div.posts. div.main div.list').bindList(); });</script>
+ <script type="text/javascript">
+ $(function(){ $('div.posts. div.main div.list').bindList(); });
+ </script>
<% }else{ %>
<p><%= _('posts.no_posts_found') %></p>
<% } %>
View
8 apps/posts/_attachments/templates/parts/post.ejs
@@ -1,4 +1,4 @@
-<article class="post"
+<article class="post <%= this.is_draft ? 'draft' : 'published' %>"
data-key="<%= this._key %>"
data-docid="<%= this._id %>"
data-docrev="<%= this._rev %>"
@@ -22,7 +22,7 @@
<%= this.updated_at %>
</time>
</footer>
+ <script type="text/javascript">
+ $(function(){$('article.post[data-docid="<%= this._id %>"]').bindDelete();});
+ </script>
</article>
-<script type="text/javascript">
-$(function(){$('article.post[data-docid="<%= this._id %>"]').bindDelete();});
-</script>
View
3  apps/posts/_attachments/templates/parts/post_editor.ejs
@@ -16,7 +16,8 @@
</div>
<div class="fieldRow">
<label for="is_draft"><%= _("posts.is_draft") %></label>
- <input id="is_draft" name="is_draft" type="checkbox" <%= this.is_draft ? 'checked="checked"' : '' %></input>
+ <input id="is_draft" name="is_draft" type="checkbox" value="true"
+ <%= this.is_draft ? 'checked="checked"' : '' %></input>
</div>
<div class="fieldRow buttons">
<button id="submit" name="submit" type="submit"><%= _("posts.submit") %></button>
View
73 apps/posts/app.js
@@ -71,6 +71,7 @@ ddoc.init = function(app, config){
config = merge({
postsPerPage: 10
}, config);
+
function makeDoc(req, oldDoc){
var now = new Date();
var doc = merge({}, ddoc.docTemplates.post, req.body,
@@ -82,6 +83,7 @@ ddoc.init = function(app, config){
doc.type = 'post';
doc.title = doc.title.toString();
doc.content = doc.content.toString();
+ doc.is_draft = doc.is_draft == 'true';
if( doc.tags ){
if( !Array.isArray(doc.tags) ){
doc.tags = doc.tags.toString().split(',');
@@ -106,6 +108,14 @@ ddoc.init = function(app, config){
return doc;
};
+ function renderList(req, res, next){
+ // TODO: content negotiation
+ res.partial('parts/posts', {
+ object: res.local('posts').data,
+ as: this
+ });
+ }
+
var m = merge(app.middleware, ddoc.middleware);
var db = app.middleware.db;
var logger = app.logger;
@@ -117,30 +127,13 @@ ddoc.init = function(app, config){
// ':id' param precondition
app.param('id', m.byId('id'));
- // -- public --
- // pages
+ // Public URIs
app.get('/',
parallel(
m.byUpdatedAt({perPage: config.postsPerPage})
),
m.feedOrHtml('index.ejs'));
- app.get('/admin/',
- function(req, res, next){
- res.render('admin/index.ejs');
- });
- app.get('/admin/new',
- function(req, res, next){
- res.local('post', ddoc.docTemplates.post);
- res.render('admin/new.ejs');
- });
-
- app.get('/admin/edit/:id',
- function(req, res, next){
- res.render('admin/edit.ejs');
- });
-
- // permalink for post entries.
app.get('/p/:id', // get an entry
m.byId('id'),
function(req, res, next){
@@ -199,22 +192,48 @@ ddoc.init = function(app, config){
}
});
});
- // apis
+
+ // Public APIs
app.get('/-/',
- m.byUpdatedAt(config),
- function(req, res, next){
- // TODO: content negotiation
- res.partial('parts/posts', {
- object: res.local('posts').data,
- as: this
- });
- });
+ m.byUpdatedAt({perPage: config.postsPerPage}),
+ renderList);
app.get('/-/count/tag',
m.countByTag());
app.get('/-/count/date',
m.countByTag());
+
+
+ // Admin URIs
+ app.get('/admin/',
+ m.byUpdatedAt({
+ perPage: config.postsPerPage,
+ includeDraft: true
+ }),
+ function(req, res, next){
+ res.render('admin/index.ejs');
+ });
+ app.get('/admin/new',
+ function(req, res, next){
+ res.local('post', ddoc.docTemplates.post);
+ res.render('admin/new.ejs');
+ });
+
+ app.get('/admin/edit/:id',
+ function(req, res, next){
+ res.render('admin/edit.ejs');
+ });
+
+
+ // Admin APIs
+ app.get('/admin/-/',
+ m.byUpdatedAt({
+ perPage: config.postsPerPage,
+ includeDraft: true
+ }),
+ renderList);
+
};
couchapp.loadAttachments(ddoc, path.join(__dirname, '_attachments'));
View
8 apps/posts/middleware.js
@@ -5,6 +5,7 @@ var allowed_names = [
'limit', 'decending', 'skip'
];
+
module.exports = {
feedOrHtml: function(filename){
return function(req, res, next){
@@ -29,9 +30,12 @@ module.exports = {
byUpdatedAt: function(options){
options = merge({
perPage: 10,
- bindAs: 'posts'
+ bindAs: 'posts',
+ includeDraft: false
}, options);
var db = this.db;
+ var viewName = 'posts/' + (options.includeDraft === true ?
+ 'all_by_updated_at' : 'by_updated_at');
return function(req, res, next){
var params = {
limit:options.perPage,
@@ -42,7 +46,7 @@ module.exports = {
params[name] = req.query[name];
}
});
- (db.bind('view', 'posts/by_updated_at', params, {
+ (db.bind('view', viewName, params, {
success: function(req, res, next){
var rows = res.locals().posts.data;
for(var i in rows){
Please sign in to comment.
Something went wrong with that request. Please try again.