Skip to content

Commit

Permalink
aggregation and backbone way
Browse files Browse the repository at this point in the history
  • Loading branch information
jollen committed Nov 15, 2014
1 parent f01727a commit 8ef86c2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ app.put('/1/user/:nickname', api.update);
app.delete('/1/user/:nickname', api.delete);

app.get('/1/user/age/:age', api.readByAge);
app.get('/1/user/age/:from/:to', api.readByAgeRange);

// Profile
app.post('/1/user/:nickname/:type', api.upload);
Expand Down
10 changes: 9 additions & 1 deletion public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ var app = app || {};
* MODELS
**/
app.Users = Backbone.Model.extend({
url: '/1/user',
url: function() {
return '/1/user/age' + this.filter;
},
filter: '/30/39',
defaults: {
errors: [],
errfor: {},
Expand All @@ -22,6 +25,7 @@ app.ListView = Backbone.View.extend({
el: '#userList',
template: _.template( $('#tmpl-user-list').html() ),
events: {
'click #btn-filter': 'click'
},
initialize: function() {
this.model = new app.Users();
Expand All @@ -32,6 +36,10 @@ app.ListView = Backbone.View.extend({
render: function() {
this.$el.html(this.template( this.model.attributes ));
},
click: function() {
this.model.filter = '/40/49';
this.model.fetch();
}
});

/**
Expand Down
31 changes: 29 additions & 2 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,36 @@ exports.readByAge = function(req, res){
var model = req.app.db.model;
var age = req.params.age;

var vcard = model.find({ Age: age }, function(err, vcard) {
//var vcard = model.find({ Age: age }, function(err, vcard) {
// res.send({
// users: vcard
// });
// res.end();
//});

model.aggregate([
{ $match: { Age: parseInt(age) } }
])
.exec(function(err, users) {
res.send({
users: vcard
users: users
});
res.end();
});
};

exports.readByAgeRange = function(req, res){
var model = req.app.db.model;
var from = parseInt(req.params.from);
var to = parseInt(req.params.to);

model.aggregate([
{ $match: { Age: {$gte: from} } },
{ $match: { Age: {$lte: to} } }
])
.exec(function(err, users) {
res.send({
users: users
});
res.end();
});
Expand Down
3 changes: 2 additions & 1 deletion views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ block content
div#userList

script(type='text/template', id='tmpl-user-list')
button.btn.btn-primary#btn-filter 40-49
h3 User List
ul
<% _.each(users, function(user) { %>
li <%= user.Name %>
li <%= user.Name %> (Age: <%= user.Age %>)
<% }); %>

script(src='jquery/dist/jquery.min.js', type='text/javascript')
Expand Down
1 change: 1 addition & 0 deletions views/layout.jade
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/bootstrap.min.css')
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content

0 comments on commit 8ef86c2

Please sign in to comment.