Skip to content

Commit

Permalink
implement #1
Browse files Browse the repository at this point in the history
  • Loading branch information
jollen committed Nov 16, 2014
1 parent b6dfa41 commit 08df97e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
33 changes: 28 additions & 5 deletions public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ var app = app || {};
**/
app.Users = Backbone.Model.extend({
url: function() {
return '/1/user/age' + this.filter;
return '/1/user';
},
filter: '/30/39',
defaults: {
errors: [],
errfor: {},
Expand All @@ -28,20 +27,44 @@ app.ListView = Backbone.View.extend({
'click #btn-filter': 'click'
},
initialize: function() {
var self = this;

this.model = new app.Users();
this.listenTo(this.model, 'sync', this.render);
this.listenTo(this.model, 'change', this.render);
this.model.fetch();
this.model.fetch({
success: function() {
var users = self.model.get('users');

users.sort(function (a, b) {
if (a.Age > b.Age) return 1;
else if (a.Age < b.Age) return -1;
return 0;
});
}
});
},
render: function() {
this.$el.html(this.template( this.model.attributes ));

this.$el.find('[data-tag=user]').each(function () {
var me = $(this);
var age = '' + me.data('age');

me.addClass('age-' + age.slice(0, 1) + '0');
});
},
click: function(e) {
var me = $(e.target);
var filter = me.data('filter');

this.model.filter = filter;
this.model.fetch();
this.$el.find('[data-tag=user]').each(function () {
$(this).addClass('hide');
});

this.$el.find('.' + filter).each(function () {
$(this).removeClass('hide');
});
}
});

Expand Down
21 changes: 21 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,25 @@ body {

a {
color: #00B7FF;
}

/**
* age range
*/
.age-20 {
}

.age-30 {
}

.age-40 {
}

.age-50 {
}

.age-60 {
}

.age-70 {
}
12 changes: 6 additions & 6 deletions views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ block content

script(type='text/template', id='tmpl-user-list')
.btn-group
button.btn.btn-primary#btn-filter(data-filter='/20/29') 20-29
button.btn.btn-primary#btn-filter(data-filter='/30/39') 30-39
button.btn.btn-primary#btn-filter(data-filter='/40/49') 40-49
button.btn.btn-primary#btn-filter(data-filter='/50/59') 50-59
button.btn.btn-primary#btn-filter(data-filter='/60/69') 60-69
button.btn.btn-primary#btn-filter(data-filter='age-20') 20-29
button.btn.btn-primary#btn-filter(data-filter='age-30') 30-39
button.btn.btn-primary#btn-filter(data-filter='age-40') 40-49
button.btn.btn-primary#btn-filter(data-filter='age-50') 50-59
button.btn.btn-primary#btn-filter(data-filter='age-60') 60-69
h3 User List
ul
<% _.each(users, function(user) { %>
li <%= user.Name %> (Age: <%= user.Age %>)
li(data-tag='user', data-age!='<%= user.Age %>') <%= user.Name %> (Age: <%= user.Age %>)
<% }); %>

script(src='jquery/dist/jquery.min.js', type='text/javascript')
Expand Down

0 comments on commit 08df97e

Please sign in to comment.