Browse Source

fix post sending

main
ghost 3 years ago
parent
commit
f0765f9ea2
  1. 70
      src/public/js/module/post.js

70
src/public/js/module/post.js

@ -1,8 +1,5 @@
var ModulePost = { var ModulePost = {
init: function(element) { loadAvatar: function() {
ModulePost.loadAvatar(element);
},
loadAvatar: function(element) {
$.ajax({ $.ajax({
url: 'api/user/avatar', url: 'api/user/avatar',
type: 'GET', type: 'GET',
@ -11,7 +8,7 @@ var ModulePost = {
if (response.success) { if (response.success) {
if (response.avatar) { if (response.avatar) {
$(element).find('img').attr('src', response.avatar).show(); $('#modulePostAvatar').attr('src', response.avatar).show();
} }
} else { } else {
@ -25,27 +22,27 @@ var ModulePost = {
} }
}); });
}, },
add: function() { send: function() {
var input = $('#modulePost .message textarea');
$.ajax({ $.ajax({
url: 'api/post/add', url: 'api/post/add',
type: 'POST', type: 'POST',
data: { data: {
message: input.val() message: $('#modulePostMessage').val()
}, },
success: function (response) { success: function (response) {
if (response.success) { if (response.success) {
input.val(''); $('#modulePostMessage').val('');
$('#modulePostPreview').hide();
$('#modulePostPreview .text').html('');
$(document).trigger('ModulePost.add:success', [response]); $(document).trigger('ModulePost.add:success', [response]);
} else { } else {
alert(response.message); console.log(response.message);
} }
}, },
error: function(jqXHR, textStatus, errorThrown) { error: function(jqXHR, textStatus, errorThrown) {
@ -53,4 +50,53 @@ var ModulePost = {
} }
}); });
}, },
} preview: function() {
$.ajax({
url: 'api/post/preview',
type: 'POST',
data: {
message: $('#modulePostMessage').val()
},
success: function (response) {
if (response.success) {
if (response.format == '') {
$('#modulePostPreview').hide();
$('#modulePostPreview .text').html('');
} else {
$('#modulePostPreview').show();
$('#modulePostPreview .text').html(response.format);
}
} else {
console.log(response.message);
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
},
}
$(document).ready(function() {
// Init module
ModulePost.loadAvatar();
// Event listeners
$(document).on('ModulePost.add:success', function(/*event, response*/) {
ModuleFeed.load('#moduleFeed', true);
});
$('#modulePostMessage').on('keyup', function() {
ModulePost.preview();
});
$('#modulePostSend').on('click', function() {
ModulePost.send();
});
});

Loading…
Cancel
Save