2013-06-25 16:23:09 +04:00
|
|
|
/*global window, document, Ghost, $, _, Backbone */
|
2013-05-31 09:58:20 +04:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
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) {
|
|
|
|
resp.published = !!(resp.status === "published");
|
|
|
|
resp.draft = !!(resp.status === "draft");
|
|
|
|
}
|
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({
|
|
|
|
url: Ghost.settings.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;
|
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
|
|
|
}());
|