client/posts: introduce post model

Now to replace all the api calls in views with event system... and then
do the same for user, tag and comment
This commit is contained in:
rr- 2016-06-13 23:28:55 +02:00
parent 6fcf81e55d
commit 2ac8d8650c
3 changed files with 93 additions and 6 deletions

View File

@ -4,6 +4,7 @@ const router = require('../router.js');
const api = require('../api.js');
const events = require('../events.js');
const settings = require('../settings.js');
const Post = require('../models/post.js');
const TopNavigation = require('../models/top_navigation.js');
const PostView = require('../views/post_view.js');
const EmptyView = require('../views/empty_view.js');
@ -26,13 +27,13 @@ class PostController {
_showPostRoute(id, editMode) {
TopNavigation.activate('posts');
Promise.all([
api.get('/post/' + id),
Post.get(id),
api.get(`/post/${id}/around?fields=id&query=` +
this._decorateSearchQuery('')),
]).then(responses => {
const [postResponse, aroundResponse] = responses;
const [post, aroundResponse] = responses;
this._postView.render({
post: postResponse,
post: post,
editMode: editMode,
nextPostId: aroundResponse.next ? aroundResponse.next.id : null,
prevPostId: aroundResponse.prev ? aroundResponse.prev.id : null,

View File

@ -6,9 +6,6 @@ const optimizedResize = require('../util/optimized_resize.js');
class PostContentControl {
constructor(containerNode, post, viewportSizeCalculator) {
post.canvasWidth = post.canvasWidth || 800;
post.canvasHeight = post.canvasHeight || 450;
this._post = post;
this._viewportSizeCalculator = viewportSizeCalculator;
this._containerNode = containerNode;

89
client/js/models/post.js Normal file
View File

@ -0,0 +1,89 @@
'use strict';
const api = require('../api.js');
const events = require('../events.js');
class Post extends events.EventTarget {
constructor() {
super();
this._id = null;
this._type = null;
this._mimeType = null;
this._creationTime = null;
this._user = null;
this._safety = null;
this._contentUrl = null;
this._thumbnailUrl = null;
this._canvasWidth = null;
this._canvasHeight = null;
this._fileSize = null;
this._tags = [];
this._notes = [];
this._comments = [];
this._relations = [];
this._score = null;
this._favoriteCount = null;
this._ownScore = null;
this._ownFavorite = null;
}
// encapsulation - don't let set these casually
get id() { return this._id; }
get type() { return this._type; }
get mimeType() { return this._mimeType; }
get creationTime() { return this._creationTime; }
get user() { return this._user; }
get safety() { return this._safety; }
get contentUrl() { return this._contentUrl; }
get thumbnailUrl() { return this._thumbnailUrl; }
get canvasWidth() { return this._canvasWidth || 800; }
get canvasHeight() { return this._canvasHeight || 450; }
get fileSize() { return this._fileSize || 0; }
get tags() { return this._tags; }
get notes() { return this._notes; }
get comments() { return this._comments; }
get relations() { return this._relations; }
get score() { return this._score; }
get favoriteCount() { return this._favoriteCount; }
get ownFavorite() { return this._ownFavorite; }
get ownScore() { return this._ownScore; }
static get(id) {
return new Promise((resolve, reject) => {
api.get('/post/' + id)
.then(response => {
const post = new Post();
post._id = response.id;
post._type = response.type;
post._mimeType = response.mimeType;
post._creationTime = response.creationTime;
post._user = response.user;
post._safety = response.safety;
post._contentUrl = response.contentUrl;
post._thumbnailUrl = response.thumbnailUrl;
post._canvasWidth = response.canvasWidth;
post._canvasHeight = response.canvasHeight;
post._fileSize = response.fileSize;
post._tags = response.tags;
post._notes = response.notes;
post._comments = response.comments;
post._relations = response.relations;
post._score = response.score;
post._favoriteCount = response.favoriteCount;
post._ownScore = response.ownScore;
post._ownFavorite = response.ownFavorite;
resolve(post);
}, response => {
reject(response);
});
});
}
};
module.exports = Post;