Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The {{#get}} helper #5619

Merged
merged 1 commit into from
Oct 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions core/server/helpers/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// # Get Helper
// Usage: `{{#get "posts" limit="5"}}`, `{{#get "tags" limit="all"}}`
// Fetches data from the API
var _ = require('lodash'),
hbs = require('express-hbs'),
Promise = require('bluebird'),
errors = require('../errors'),
api = require('../api'),
resources,
get;

// Endpoints that the helper is able to access
resources = ['posts', 'tags', 'users'];

/**
* ## Is Browse
* Is this a Browse request or a Read request?
* @param {Object} context
* @param {Object} options
* @returns {boolean}
*/
function isBrowse(context, options) {
var browse = true;

if (options.id || options.slug) {
browse = false;
}

return browse;
}

/**
* ## Parse Options
* Ensure options passed in make sense
*
* @param {Object} data
* @param {Object} options
* @returns {*}
*/
function parseOptions(data, options) {
if (_.isArray(options.tag)) {
options.tag = _.pluck(options.tag, 'slug').join(',');
}

if (_.isObject(options.author)) {
options.author = options.author.slug;
}

return options;
}

/**
* ## Get
* @param {Object} context
* @param {Object} options
* @returns {Promise}
*/
get = function get(context, options) {
options = options || {};
options.hash = options.hash || {};
options.data = options.data || {};

var self = this,
data = hbs.handlebars.createFrame(options.data),
apiOptions = _.omit(options.hash, 'context'),
apiMethod;

if (!options.fn) {
data.error = 'Get helper must be called as a block';
errors.logWarn(data.error);
return Promise.resolve();
}

if (!_.contains(resources, context)) {
data.error = 'Invalid resource given to get helper';
errors.logWarn(data.error);
return Promise.resolve(options.inverse(self, {data: data}));
}

// Determine if this is a read or browse
apiMethod = isBrowse(context, apiOptions) ? api[context].browse : api[context].read;
// Parse the options we're going to pass to the API
apiOptions = parseOptions(this, apiOptions);

return apiMethod(apiOptions).then(function success(result) {
result = _.merge(self, result);
if (_.isEmpty(result[context])) {
return options.inverse(self, {data: data});
}

return options.fn(result, {
data: data,
blockParams: [result[context]]
});
}).catch(function error(err) {
data.error = err.message;
return options.inverse(self, {data: data});
});
};

module.exports = get;
2 changes: 2 additions & 0 deletions core/server/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ coreHelpers.date = require('./date');
coreHelpers.encode = require('./encode');
coreHelpers.excerpt = require('./excerpt');
coreHelpers.foreach = require('./foreach');
coreHelpers.get = require('./get');
coreHelpers.ghost_foot = require('./ghost_foot');
coreHelpers.ghost_head = require('./ghost_head');
coreHelpers.image = require('./image');
Expand Down Expand Up @@ -117,6 +118,7 @@ registerHelpers = function (adminHbs) {
registerAsyncThemeHelper('post_class', coreHelpers.post_class);
registerAsyncThemeHelper('next_post', coreHelpers.next_post);
registerAsyncThemeHelper('prev_post', coreHelpers.prev_post);
registerAsyncThemeHelper('get', coreHelpers.get);

// Register admin helpers
registerAdminHelper('asset', coreHelpers.asset);
Expand Down
Loading