Skip to content

Commit

Permalink
list posts: implement UserPost view, #9
Browse files Browse the repository at this point in the history
  • Loading branch information
jollen committed May 29, 2016
1 parent 953fade commit a58f823
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ app.post('/1/user/:nickname/:type', api.upload);

app.post('/1/post', api.createPost);
app.get('/1/post', api.readPost);
app.get('/1/post/:id', api.readPostById);


http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
Expand Down
31 changes: 31 additions & 0 deletions public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ app.Post = Backbone.Model.extend({
}
});

app.UserPost = Backbone.Model.extend({
url: function() {
return 'http://localhost:3000/1/post/' + this.attributes.uid;
},
defaults: {
errors: [],
errfor: {},

uid: '',
posts: []
}
});

app.UserCollection = Backbone.Collection.extend({
model: app.UserInfo
});
Expand Down Expand Up @@ -115,6 +128,20 @@ app.ListView = Backbone.View.extend({
}
});

app.UserPostView = Backbone.View.extend({
el: '#userPost',
template: _.template( $('#tmpl-user-posts').html() ),
initialize: function(id) {
this.model = new app.UserPost();

this.listenTo(this.model, 'sync', this.render);
this.listenTo(this.model, 'change', this.render);
},
render: function() {
this.$el.html(this.template( this.model.attributes ));
}
});

app.UserView = Backbone.View.extend({
template: _.template( $('#tmpl-user-info').html() ),
events: {
Expand Down Expand Up @@ -180,6 +207,7 @@ app.UserViewPanel = Backbone.View.extend({
el: '#userInfo',
views: [],
initialize: function() {
this.userPostView = new app.UserPostView();
},
invalidate: function() {
console.log('views: ', this.views);
Expand Down Expand Up @@ -217,6 +245,9 @@ app.UserViewPanel = Backbone.View.extend({
});

this.$el.append( childView.render().$el );

this.userPostView.model.set('uid', id);
this.userPostView.model.fetch();
} else {
this.$el.find('#' + id).removeClass('hide');
}
Expand Down
14 changes: 14 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ exports.read = function(req, res){
});
};

exports.readPostById = function(req, res){
var model = req.app.db.model.Post;

var vcard = model.find({
uid: req.params.id
}, function(err, posts) {
res.send({
posts: posts
});

res.end();
});
};

exports.readOneByUserId = function(req, res){
var model = req.app.db.model.User;
var id = req.params.id;
Expand Down
9 changes: 8 additions & 1 deletion views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ block content
h2 User Info
hr
div#userInfo
div#userPost

script(type='text/template', id='tmpl-user-list')
.btn-group
Expand All @@ -28,6 +29,12 @@ block content
td <%= user.Age %>
<% }); %>

script(type='text/template', id='tmpl-user-posts')
ul
<% _.each(posts, function(post) { %>
li <%= post.title %>
<% }); %>

script(type='text/template', id='tmpl-user-info')
div.well.non-editable
.row
Expand Down Expand Up @@ -55,7 +62,7 @@ block content
label.control-label Content:
div.controls
input.form-control(type='text', name='content', value='')
input(type='hidden', name='id', value='<%= user._id %>')
input(type='hidden', name='id', value!='<%= user._id %>')
button.btn.btn-primary.pull-left.btn-post-submit Submit Message
div.well.editable.hide(style='margin-top: 50px;')
.row
Expand Down

0 comments on commit a58f823

Please sign in to comment.