2013-06-25 16:23:09 +04:00
|
|
|
/*global window, document, Ghost, $, _, Backbone */
|
2013-05-31 09:58:20 +04:00
|
|
|
(function () {
|
2013-09-24 14:46:30 +04:00
|
|
|
'use strict';
|
2013-05-31 09:58:20 +04:00
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
Ghost.Models.Post = Backbone.Model.extend({
|
|
|
|
|
2013-05-31 09:58:20 +04:00
|
|
|
defaults: {
|
|
|
|
status: 'draft'
|
2013-06-02 03:45:02 +04:00
|
|
|
},
|
|
|
|
|
2013-07-07 22:41:05 +04:00
|
|
|
blacklist: ['published', 'draft'],
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
parse: function (resp) {
|
2013-07-04 22:42:49 +04:00
|
|
|
if (resp.status) {
|
2013-09-24 14:46:30 +04:00
|
|
|
resp.published = !!(resp.status === 'published');
|
|
|
|
resp.draft = !!(resp.status === 'draft');
|
2013-07-04 22:42:49 +04:00
|
|
|
}
|
2013-06-02 03:45:02 +04:00
|
|
|
if (resp.tags) {
|
|
|
|
// TODO: parse tags into it's own collection on the model (this.tags)
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
return resp;
|
|
|
|
},
|
|
|
|
|
|
|
|
validate: function (attrs) {
|
|
|
|
if (_.isEmpty(attrs.title)) {
|
|
|
|
return 'You must specify a title for the post.';
|
|
|
|
}
|
2013-08-21 16:55:58 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
2013-05-31 09:58:20 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
Ghost.Collections.Posts = Backbone.Collection.extend({
|
2013-09-16 03:34:23 +04:00
|
|
|
currentPage: 1,
|
|
|
|
totalPages: 0,
|
|
|
|
totalPosts: 0,
|
|
|
|
nextPage: 0,
|
|
|
|
prevPage: 0,
|
|
|
|
|
2013-09-15 15:13:06 +04:00
|
|
|
url: Ghost.settings.apiRoot + '/posts/',
|
2013-06-02 03:45:02 +04:00
|
|
|
model: Ghost.Models.Post,
|
2013-09-16 03:34:23 +04:00
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
parse: function (resp) {
|
|
|
|
if (_.isArray(resp.posts)) {
|
|
|
|
this.limit = resp.limit;
|
|
|
|
this.currentPage = resp.page;
|
|
|
|
this.totalPages = resp.pages;
|
|
|
|
this.totalPosts = resp.total;
|
2013-06-25 19:13:19 +04:00
|
|
|
this.nextPage = resp.next;
|
|
|
|
this.prevPage = resp.prev;
|
2013-06-02 03:45:02 +04:00
|
|
|
return resp.posts;
|
|
|
|
}
|
|
|
|
return resp;
|
|
|
|
}
|
2013-05-31 09:58:20 +04:00
|
|
|
});
|
|
|
|
|
2013-08-21 16:55:58 +04:00
|
|
|
}());
|