mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-13 14:39:52 +03:00
254b6618f3
closes #2609 - added include parameter to api.posts.* - changed toJSON to omit objects that are not included - added include parameter to admin - added include parameter to frontend.js - updated tests - removed duplicate code from posts model **Known Issue:** It is not possible to attach a tag using an ID.
84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
/*global Ghost, _, Backbone, JSON */
|
|
(function () {
|
|
'use strict';
|
|
|
|
Ghost.Models.Post = Ghost.ProgressModel.extend({
|
|
|
|
defaults: {
|
|
status: 'draft'
|
|
},
|
|
|
|
blacklist: ['published', 'draft'],
|
|
|
|
parse: function (resp) {
|
|
|
|
if (resp.posts) {
|
|
resp = resp.posts[0];
|
|
}
|
|
if (resp.status) {
|
|
resp.published = resp.status === 'published';
|
|
resp.draft = resp.status === 'draft';
|
|
}
|
|
if (resp.tags) {
|
|
return resp;
|
|
}
|
|
return resp;
|
|
},
|
|
|
|
validate: function (attrs) {
|
|
if (_.isEmpty(attrs.title)) {
|
|
return 'You must specify a title for the post.';
|
|
}
|
|
},
|
|
|
|
addTag: function (tagToAdd) {
|
|
var tags = this.get('tags') || [];
|
|
tags.push(tagToAdd);
|
|
this.set('tags', tags);
|
|
},
|
|
|
|
removeTag: function (tagToRemove) {
|
|
var tags = this.get('tags') || [];
|
|
tags = _.reject(tags, function (tag) {
|
|
return tag.id === tagToRemove.id || tag.name === tagToRemove.name;
|
|
});
|
|
this.set('tags', tags);
|
|
},
|
|
sync: function (method, model, options) {
|
|
//wrap post in {posts: [{...}]}
|
|
if (method === 'create' || method === 'update') {
|
|
options.data = JSON.stringify({posts: [this.attributes]});
|
|
options.contentType = 'application/json';
|
|
options.url = model.url() + '?include=tags';
|
|
}
|
|
|
|
return Backbone.Model.prototype.sync.apply(this, arguments);
|
|
}
|
|
});
|
|
|
|
Ghost.Collections.Posts = Backbone.Collection.extend({
|
|
currentPage: 1,
|
|
totalPages: 0,
|
|
totalPosts: 0,
|
|
nextPage: 0,
|
|
prevPage: 0,
|
|
|
|
url: Ghost.paths.apiRoot + '/posts/',
|
|
model: Ghost.Models.Post,
|
|
|
|
parse: function (resp) {
|
|
if (_.isArray(resp.posts)) {
|
|
this.limit = resp.meta.pagination.limit;
|
|
this.currentPage = resp.meta.pagination.page;
|
|
this.totalPages = resp.meta.pagination.pages;
|
|
this.totalPosts = resp.meta.pagination.total;
|
|
this.nextPage = resp.meta.pagination.next;
|
|
this.prevPage = resp.meta.pagination.prev;
|
|
return resp.posts;
|
|
}
|
|
return resp;
|
|
}
|
|
});
|
|
|
|
}());
|