mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
9321289c1d
closes #2580 - added new format to post API methods - added post object parsing and wrapping to admin - removed unused ‚user‘ object from API response - updated tests
82 lines
2.3 KiB
JavaScript
82 lines
2.3 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';
|
|
}
|
|
|
|
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.limit;
|
|
this.currentPage = resp.page;
|
|
this.totalPages = resp.pages;
|
|
this.totalPosts = resp.total;
|
|
this.nextPage = resp.next;
|
|
this.prevPage = resp.prev;
|
|
return resp.posts;
|
|
}
|
|
return resp;
|
|
}
|
|
});
|
|
|
|
}());
|