Skip to content

Commit

Permalink
Add pagination to episode list
Browse files Browse the repository at this point in the history
  • Loading branch information
maritz committed Mar 26, 2012
1 parent 35f6428 commit a0ccf1e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 68 deletions.
80 changes: 51 additions & 29 deletions controllers/EpisodeController.js
Expand Up @@ -20,49 +20,71 @@ function EpisodeError(msg, code){
EpisodeError.prototype.__proto__ = Error.prototype;


var profiler = require('v8-profiler');

app.get('/byShow/:id', auth.isLoggedIn, auth.may('list', 'Episode'), loadModel('Show'), function (req, res, next) {
var season = req.param('season');
console.time('getting ids');
profiler.startProfiling('startup');
var cb = function (err, ids) {
console.timeEnd('getting ids');
var start = parseInt(req.param('offset'), 10) || 0;
var max = 30;
var more_after_max = false;
var total = 0;

var loadEpisodes = function (ids) {
async.map(ids, function (id, cb) {
nohm.factory('Episode', id, function (err, data) {
if (err) {
cb(err);
} else {
req.user.belongsTo(this, 'seen', function (err, belongs) {
if (err) {
cb(err);
}
data.seen = belongs;
data.id = id;
cb(null, data);
});
}
});
}, function (err, episodes) {
if (err) {
console.log(err);
next(new EpisodeError('Error while loading the episodes.'));
} else {
res.ok({
total: total,
per_page: max,
collection: episodes
});
}
});
};

var getAllCallback = function (err, ids) {
if (err) {
next(new EpisodeError('Error while retreiving the episode ids.'));
} else {
console.time('loading all');
async.map(ids, function (id, cb) {
nohm.factory('Episode', id, function (err, data) {
total = ids.length;
if (total > max) {
more_after_max = (start+max > total);
Episode.sort({
field: 'number',
limit: [start, max]
}, ids,
function (err, ids) {
if (err) {
cb(err);
next(new EpisodeError('Error while sorting the episode ids.'));
} else {
req.user.belongsTo(this, 'seen', function (err, belongs) {
if (err) {
cb(err);
}
data.seen = belongs;
data.id = id;
cb(null, data);
});
loadEpisodes(ids);
}
});
}, function (err, episodes) {
console.timeEnd('loading all');
profiler.stopProfiling('startup');
if (err) {
console.log(err);
next(new EpisodeError('Error while loading the episodes.'));
} else {
res.ok(episodes);
}
});
} else {
loadEpisodes(ids);
}
}
};
if (season) {
req.loaded.Show.getAll('Episode', 'season'+season, cb);
req.loaded.Show.getAll('Episode', 'season'+season, getAllCallback);
} else {
req.loaded.Show.getAll('Episode', cb);
req.loaded.Show.getAll('Episode', getAllCallback);
}
});

Expand Down
3 changes: 2 additions & 1 deletion models/EpisodeModel.js
Expand Up @@ -18,7 +18,8 @@ module.exports = nohm.model('Episode', {
type: 'integer'
},
number: {
type: 'integer'
type: 'integer',
index: true
},
first_aired: {
type: 'timestamp'
Expand Down
Binary file modified static/images/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions static/js/collections/episodeCollection.js
Expand Up @@ -4,6 +4,8 @@ _r(function (app) {
model: app.models.Episode,
url: '/REST/Episode/',

pagination_by_field: 'number',

comparator: function (episode) {
return episode.get('number');
},
Expand Down
83 changes: 45 additions & 38 deletions static/templates/tmpl-show.html
Expand Up @@ -71,7 +71,7 @@

h2
= data.get('name')

ul.show_details
li
- var genres = data.get('genre').replace(/(^\|)|(\|$)/g, '').split('|');
Expand Down Expand Up @@ -124,44 +124,51 @@
.season_seen_container
!=partial('season_seen_button', data)

ul.episode_list
each episode in data.models
li.well.span3.episode_detail(data-id: episode.id)
h3
if parseInt(episode.get('season'), 10) == 0
=episode.get('name')
else
=_t('episode')+' '+episode.get('number')

if data.paginated
.pagination.pagination-centered

ul.episode_list.pagination_content
!=partial('episode_details', data.models)
</script>

<script type="text/x-jade-tmpl" name="episode_details">
each episode in args[0]
li.well.span3.episode_detail(data-id: episode.id)
h3
if parseInt(episode.get('season'), 10) == 0
=episode.get('name')
else
=_t('episode')+' '+episode.get('number')

label
= _t('name')
span.one_line= episode.get('name')

br
- var date = moment(episode.get('first_aired'));
time(datetime:date.format('YYYY-MM-DD'), title:date.format('MMM Do YYYY'))
label
=_t('air_date')
=date.fromNow()

br
.episode_seen_container
!=partial('episode_seen_button', episode)

a.episode_opener(href: '#')
=_t('more')

.episode_details.hidden

label
= _t('plot')
= episode.get('plot')

br
if episode.get('imdb_link')
label
= _t('name')
span.one_line= episode.get('name')

br
- var date = moment(episode.get('first_aired'));
time(datetime:date.format('YYYY-MM-DD'), title:date.format('MMM Do YYYY'))
label
=_t('air_date')
=date.fromNow()

br
.episode_seen_container
!=partial('episode_seen_button', episode)

a.episode_opener(href: '#')
=_t('more')

.episode_details.hidden

label
= _t('plot')
= episode.get('plot')

br
if episode.get('imdb_link')
label
=_t('imdb_link')+':'
a(href:'http://imdb.com/title/'+episode.get('imdb_id'), target: episode.get('name')+' IMDB')= episode.get('imdb_id')
=_t('imdb_link')+':'
a(href:'http://imdb.com/title/'+episode.get('imdb_id'), target: episode.get('name')+' IMDB')= episode.get('imdb_id')
</script>

<script type="text/x-jade-tmpl" name="episode_seen_button">
Expand Down

0 comments on commit a0ccf1e

Please sign in to comment.