diff --git a/app.js b/app.js index 7987f03..fcbdc82 100644 --- a/app.js +++ b/app.js @@ -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')); diff --git a/public/index.js b/public/index.js index 7007173..5158ea5 100644 --- a/public/index.js +++ b/public/index.js @@ -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 }); @@ -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: { @@ -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); @@ -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'); } diff --git a/routes/api.js b/routes/api.js index 8672822..20f1a6e 100644 --- a/routes/api.js +++ b/routes/api.js @@ -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; diff --git a/views/index.jade b/views/index.jade index 2dbc723..468c9b3 100644 --- a/views/index.jade +++ b/views/index.jade @@ -9,6 +9,7 @@ block content h2 User Info hr div#userInfo + div#userPost script(type='text/template', id='tmpl-user-list') .btn-group @@ -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 @@ -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