server side half of #83

This commit is contained in:
Tim Griesser 2013-06-01 15:30:42 -04:00
parent d4f43c294c
commit c55a96083a
4 changed files with 41 additions and 16 deletions

View File

@ -24,7 +24,7 @@
// takes filter / pagination parameters
// returns a list of posts in a json response
browse: function (options) {
return dataProvider.Post.findAll(options);
return dataProvider.Post.findPage(options);
},
// takes an identifier (id or slug?)
// returns a single post in a json response

View File

@ -16,6 +16,10 @@
hasTimestamps: true,
defaults: {
status: 'draft'
},
initialize: function () {
this.on('creating', this.creating, this);
this.on('saving', this.saving, this);
@ -69,17 +73,24 @@
var postCollection;
// Allow findPage(n)
if (!_.isObject(opts)) {
if (_.isString(opts) || _.isNumber(opts)) {
opts = {page: opts};
}
opts = _.defaults(opts || {}, {
page: 1,
opts = _.extend({page: 1}, {
limit: 15,
where: null
});
where: {},
status: 'published'
}, opts);
postCollection = Posts.forge();
// Unless `all` is passed as an option, filter on
// the status provided.
if (opts.status !== 'all') {
opts.where.status = opts.status;
}
// If there are where conditionals specified, add those
// to the query.
if (opts.where) {
@ -93,7 +104,7 @@
return postCollection
.query('limit', opts.limit)
.query('offset', opts.limit * (opts.page - 1))
.fetch(_.omit(opts, 'page', 'limit', 'where'))
.fetch(_.omit(opts, 'page', 'limit', 'where', 'status'))
.then(function (collection) {
var qb;

View File

@ -72,7 +72,7 @@
}).then(null, done);
});
it('can add', function (done) {
it('can add, defaulting as a draft', function (done) {
var newPost = {
title: 'Test Title 1',
content: 'Test Content 1'
@ -81,9 +81,10 @@
PostModel.add(newPost).then(function (createdPost) {
should.exist(createdPost);
createdPost.attributes.title.should.equal(newPost.title, "title is correct");
createdPost.attributes.content.should.equal(newPost.content, "content is correct");
createdPost.attributes.slug.should.equal(newPost.title.toLowerCase().replace(/ /g, '-'), 'slug is correct');
createdPost.get('status').should.equal('draft');
createdPost.get('title').should.equal(newPost.title, "title is correct");
createdPost.get('content').should.equal(newPost.content, "content is correct");
createdPost.get('slug').should.equal(newPost.title.toLowerCase().replace(/ /g, '-'), 'slug is correct');
done();
}).then(null, done);
@ -173,6 +174,12 @@
paginationResult.pages.should.equal(3);
return PostModel.findPage({limit: 10, page: 2, status: 'all'});
}).then(function (paginationResult) {
paginationResult.pages.should.equal(11);
done();
}).then(null, done);

File diff suppressed because one or more lines are too long