mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Merge pull request #5180 from ErisDS/issue-5177
Fix previous and next helpers
This commit is contained in:
commit
599af34ba8
2 changed files with 13 additions and 7 deletions
|
@ -8,9 +8,10 @@ var api = require('../api'),
|
||||||
Promise = require('bluebird'),
|
Promise = require('bluebird'),
|
||||||
fetch, prevNext;
|
fetch, prevNext;
|
||||||
|
|
||||||
fetch = function (options) {
|
fetch = function (apiOptions, options) {
|
||||||
return api.posts.read(options).then(function (result) {
|
return api.posts.read(apiOptions).then(function (result) {
|
||||||
var related = result.posts[0];
|
var related = result.posts[0];
|
||||||
|
|
||||||
if (related.previous) {
|
if (related.previous) {
|
||||||
return options.fn(related.previous);
|
return options.fn(related.previous);
|
||||||
} else if (related.next) {
|
} else if (related.next) {
|
||||||
|
@ -26,10 +27,14 @@ fetch = function (options) {
|
||||||
|
|
||||||
prevNext = function (options) {
|
prevNext = function (options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
options.include = options.name === 'prev_post' ? 'previous' : 'next';
|
|
||||||
|
var apiOptions = {
|
||||||
|
include: options.name === 'prev_post' ? 'previous' : 'next'
|
||||||
|
};
|
||||||
|
|
||||||
if (schema.isPost(this)) {
|
if (schema.isPost(this)) {
|
||||||
options.slug = this.slug;
|
apiOptions.slug = this.slug;
|
||||||
return fetch(options);
|
return fetch(apiOptions, options);
|
||||||
} else {
|
} else {
|
||||||
return Promise.resolve(options.inverse(this));
|
return Promise.resolve(options.inverse(this));
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,6 +138,7 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||||
var attrs = _.extend({}, this.attributes),
|
var attrs = _.extend({}, this.attributes),
|
||||||
self = this;
|
self = this;
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
options = _.pick(options, ['shallow', 'baseKey', 'include', 'context']);
|
||||||
|
|
||||||
if (options && options.shallow) {
|
if (options && options.shallow) {
|
||||||
return attrs;
|
return attrs;
|
||||||
|
@ -150,9 +151,9 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
||||||
_.each(this.relations, function (relation, key) {
|
_.each(this.relations, function (relation, key) {
|
||||||
if (key.substring(0, 7) !== '_pivot_') {
|
if (key.substring(0, 7) !== '_pivot_') {
|
||||||
// if include is set, expand to full object
|
// if include is set, expand to full object
|
||||||
var fullKey = _.isEmpty(options.name) ? key : options.name + '.' + key;
|
var fullKey = _.isEmpty(options.baseKey) ? key : options.baseKey + '.' + key;
|
||||||
if (_.contains(self.include, fullKey)) {
|
if (_.contains(self.include, fullKey)) {
|
||||||
attrs[key] = relation.toJSON(_.extend({}, options, {name: fullKey, include: self.include}));
|
attrs[key] = relation.toJSON(_.extend({}, options, {baseKey: fullKey, include: self.include}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue