Skip to content

Instantly share code, notes, and snippets.

@fuzzmz

fuzzmz/posts.js Secret

Created October 1, 2014 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzzmz/315e2c73ec04137204bf to your computer and use it in GitHub Desktop.
Save fuzzmz/315e2c73ec04137204bf to your computer and use it in GitHub Desktop.
Updated posts.js to expose old style JSON data
/**
* ### Read
* Find a post, by ID or Slug
*
* @public
* @param {{id_or_slug (required), context, status, include, ...}} options
* @return {Promise(Post)} Post
*/
read: function read(options) {
var attrs = ['id', 'slug', 'status'],
data = _.pick(options, attrs);
options = _.omit(options, attrs);
// only published posts if no user is present
if (!(options.context && options.context.user)) {
data.status = 'published';
}
if (options.include) {
options.include = prepareInclude(options.include);
}
return dataProvider.Post.findOne(data, options).then(function (result) {
if (result) {
return {posts: [result.toJSON()]};
}
return Promise.reject(new errors.NotFoundError('Post not found.'));
});
},
readN: function readN(options) {
var attrs = ['id', 'slug', 'status'],
data = _.pick(options, attrs);
options = _.omit(options, attrs);
// only published posts if no user is present
if (!(options.context && options.context.user)) {
data.status = 'published';
}
if (options.include) {
options.include = prepareInclude(options.include);
}
return dataProvider.Post.findOne(data, options).then(function (result) {
if (result) {
return result.toJSON();
}
return Promise.reject(new errors.NotFoundError('Post not found.'));
});
},
/**
* ### Edit
* Update properties of a post
*
* @public
* @param {Post} object Post or specific properties to update
* @param {{id (required), context, include,...}} options
* @return {Promise(Post)} Edited Post
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment