在 Ajax 中成功提交注释后重新加载或刷新页面



我正在尝试在成功提交评论后重新加载页面 通过 Ajax 提交的评论

在此行之后

// Save the comment
$state = $comment->store();

如果我尝试使用 location.reload((; as

// Save the comment
$state = $comment->store();
location.reload();

然后它不起作用,评论甚至没有提交 下面是完整的代码 任何人都可以提供帮助 - 当评论成功提交并存储在后端时,如何刷新和加载页面

<?php
defined('_JEXEC') or die('Unauthorized Access');
require_once(JPATH_COMPONENT . '/views/views.php');
class EasyBlogViewComments extends EasyBlogView
{
/**
* Processes comment saving
*
* @since   4.0
* @access  public
* @param   string
* @return
*/
public function save()
{
// Check for request forgeries
EB::checkToken();
// Test if user is really allowed to post comments
if (!$this->acl->get('allow_comment')) {
return $ajax->reject(JText::_('COM_EASYBLOG_NO_PERMISSION_TO_POST_COMMENT'));
}
// Default values
$moderated = false;
$parentId = $this->input->get('parentId', 0, 'int');
$depth = $this->input->get('depth', 0, 'int');
$subscribe = $this->input->get('subscribe', false, 'bool');
$email = $this->input->get('email', '', 'email');
$message = $this->input->get('comment', '', 'default');
$name = $this->input->get('name', '', 'default');
$username = $this->input->get('username', '', 'default');
$password = $this->input->get('password', '', 'default');
$title = $this->input->get('title', '', 'default');
$terms = $this->input->get('terms', false, 'bool');
$blogId = $this->input->get('blogId', 0, 'int');
$isCB = $this->input->get('iscb', 0, 'int');
// If there is no name, and the current user is logged in, use their name instead
if (!$name && $this->my->id) {
$user = EB::user($this->my->id);
$name = $user->getName();
}
// Validate the email
$data = array('post_id' => $blogId, 'comment' => $message, 'title' => $title, 'email' => $email, 'name' => $name, 'username' => $username, 'terms' => $terms);
// Load up comment table
$comment = EB::table('Comment');
$state = $comment->validatePost($data);
if (!$state) {
return $this->ajax->reject($comment->getError());
}
// Bind the data on the comment table now
$comment->bindPost($data);
// Check for spams
if ($comment->isSpam()) {
return $this->ajax->reject(JText::_('COM_EASYBLOG_SPAM_DETECTED_IN_COMMENT'));
}
$captchaResponse = EB::captcha()->verify();
// Perform captcha verification
if (isset($captchaResponse->success) && $captchaResponse->success == false) {
return $this->ajax->reject($captchaResponse->errorCodes);
}
// Get current date
$date = EB::date();
// Set other attributes for the comment
$comment->created = $date->toSql();
$comment->modified = $date->toSql();
$comment->published = true;
$comment->parent_id = $parentId;
$comment->created_by = $this->my->id;
// Process user registrations via comment
$register = $this->input->get('register', '', 'bool');
if ($register && $this->my->guest) {
if (empty($password) || empty($username) || empty($email)) {
return $this->ajax->reject('COM_EASYBLOG_COMMENT_REGISTRATION_FIELD_EMPTY');
}
$userModel = EB::model('Users');
$id = $userModel->createUser($username, $email, $name, $password);
if (!is_numeric($id)) {
return $this->ajax->reject($id);
}
$comment->created_by = $id;
}
$totalComments = $this->input->get('totalComment', 0, 'int');

// Determines if comment moderation is enabled
if ($this->config->get('comment_moderatecomment') == 1 || ($this->my->guest && $this->config->get('comment_moderateguestcomment'))) {
$comment->published = EBLOG_COMMENT_STATUS_MODERATED;
}
// Load up the blog table
$blog = EB::table('Blog');
$blog->load($comment->post_id);
// If moderation for author is disabled, ensure that the comment is also published automatically.
if ((!$this->config->get('comment_moderateauthorcomment') && $blog->created_by == $this->my->id) || EB::isSiteAdmin()) {
$comment->published = true;
}
// Update the ordering of the comment before storing
$comment->updateOrdering();
// Save the comment
$state = $comment->store();

if (!$state) {
return $this->ajax->reject($comment->getError());
}
$resultMessage = JText::_('COM_EASYBLOG_COMMENTS_POSTED_SUCCESS');
$resultState = 'success';

// If user registered as well, display a proper message
if ($register) {
$resultMessage = JText::_('COM_EASYBLOG_COMMENTS_SUCCESS_AND_REGISTERED');
}
if ($comment->isModerated()) {
$resultMessage = JText::_('COM_EASYBLOG_COMMENT_POSTED_UNDER_MODERATION');
$resultState = 'info';
}
// Process comment subscription
if ($subscribe && $this->config->get('main_subscription') && $blog->subscription) {
$subscribeModel = EB::model('Subscription');
$subscribeModel->subscribe('blog', $blog->id, $email, $name, $this->my->id);
}
// Process comment notifications
$comment->processEmails($comment->isModerated(), $blog);
// Set the comment depth
$comment->depth = $this->input->get('depth', 0, 'int');
// Update the sent flag
$comment->updateSent();
// Format the comments
$result = EB::comment()->format(array($comment));
$comment = $result[0];
$language = JFactory::getLanguage();
$rtl = $language->isRTL();
$theme = EB::template();
$theme->set('comment', $comment);
$theme->set('rtl', $rtl);
$output = '';
if ($isCB) {
// if the is saving from CB plugin, then we need to display the output using different template.
$output = $theme->output('site/comments/cb.item');
} else {
$output = $theme->output('site/comments/default.item');
}
return $this->ajax->resolve($output, $resultMessage, $resultState);
}
public function reloadCaptcha()
{
$ajax = EB::ajax();
// Get the previous captcha id.
$id   = $this->input->get('previousId', 0, 'int');
$captcha = EB::table('Captcha');
$state = $captcha->load($id);
if ($state) {
$captcha->delete();
}
// Generate a new captcha
$captcha = EB::table('Captcha');
$captcha->created = EB::date()->toSql();
$captcha->store();
$image = EB::_('index.php?option=com_easyblog&task=captcha.generate&tmpl=component&no_html=1&id=' . $captcha->id, false);
return $ajax->resolve($image, $captcha->id);
}

第二个文件

EasyBlog.module('comments/form', function($) {
var module = this;
EasyBlog.require()
.script('comments/captcha', 'comments/list')
.library('markitup')
.done(function($) {
EasyBlog.Controller('Comments.Form', {
defaultOptions: {
"{formWrapper}": "[data-comment-form-wrapper]",
"{form}": "[data-comment-form]",
"{title}": "[data-comment-title]",
"{name}": "[data-comment-name]",
"{username}": "[data-comment-username]",
"{password}": "[data-comment-password]",
"{email}": "[data-comment-email]",
"{register}": "[data-comment-register]",
"{website}": "[data-comment-website]",
"{counter}": "[data-comment-counter]",
"{subscribe}" : "[data-comment-subscribe]",
"{terms}": "[data-comment-terms]",
"{tncLink}": "[data-comment-tnc]",
"{parentId}" : "[data-comment-parent-id]",
"{commentDepth}": "[data-comment-depth]",
"{blogId}" : "[data-comment-blog-id]",
"{depth}": "[data-comment-depth]",
"{notice}": "[data-comment-notice]",
"{editor}": "[data-comment-editor]",
"{submit}": "[data-comment-submit]",
"{formToken}": "[data-comment-token]",
"{recaptcha}": "[data-recaptcha-item]"
}
}, function(self, opts, base) {
return {
init: function() {
self.initEditor();
self.list = self.addPlugin('list');
// If recaptcha is enabled, we should skip the normal captcha
var recaptcha = self.recaptcha.inside(self.element).length;
if (recaptcha < 1) {
self.captcha = self.addPlugin('captcha');
}
},
initEditor: function() {
if (self.editor().data('comment-bbcode') == 1) {
self.editor().markItUp(window.EasyBlogBBCodeSettings);
}
},
setNotice: function(message, type) {
var className = '';
if (type == 'error') {
className = 'alert-danger';
}
if (type == 'success') {
className = 'alert-success';
}
if (type == 'info') {
className = 'alert-info';
}
self.notice()
.removeClass('hide')
.addClass('alert ' + className)
.html(message);
},
resetForm: function() {
// If the comment form has a parent id, we need to reposition the comment form back.
var parentId = self.parentId().val();
if (parentId != 0) {
self.form().appendTo(self.formWrapper());
}
// Reset the form
self.username().val('');
self.password().val('');
self.subscribe().attr('checked', false);
self.editor().val('');
self.website().val('');
self.name().val('');
self.depth().val(0);
self.parentId().val(0);
self.trigger('resetForm');
// Determine if recaptcha is available
var recaptcha = self.recaptcha.inside(self.element);
// Get recaptcha's response
if (recaptcha.length > 0) {
grecaptcha.reset();
}
},
resetNotice: function() {
self.notice()
.removeClass('info error')
.html('');
},
"{self} replyComment": function(el, event, commentItem, commentId, commentDepth) {
// Hide notices in the reply form
self.notice().addClass('hide');
// When user tries to reply to an existing comment, move the form next to the level of the comment item
commentItem.after(self.form());
self.depth().val(commentDepth);
// Set the new parent id to the comment's id
self.parentId().val(commentId);
},
"{self} cancelReply": function(el, event, commentItem, commentId) {
// Set the parent id to 0
self.parentId().val(0);
// Reset the comment depth back to 0
self.depth().val(0);
// Relocate the form back to it's origin
self.formWrapper().html(self.form());
},
"{self} commentAdded": function()
{
// Increment the counter
var count = self.counter().html();
count = parseInt(count) + 1;
self.counter().html(count.toString());
self.resetForm();
},
getValues: function() {
var data = {
title: self.title().val(),
name: self.name().val(),
email: self.email().val(),
username: self.username().val(),
password: self.password().val(),
website: self.website().val(),
subscribe: self.subscribe().is(':checked') ? 1 : 0,
register: self.register().is(':checked') ? 1 : 0,
comment: self.editor().val(),
terms: self.terms().is(':checked') ? 1 : 0,
depth: self.depth().val(),
parentId: self.parentId().val(),
blogId: self.blogId().val()
};
// token
// data[self.formToken().attr('name')] = 1;
// Determine if recaptcha is available
var recaptcha = self.recaptcha.inside(self.element);
// Get recaptcha's response
if (recaptcha.length > 0) {
data.recaptcha = grecaptcha.getResponse();
}
self.trigger('submitComment', [data]);
return data;
},
"{tncLink} click": function() {
EasyBlog.dialog({
content: EasyBlog.ajax('site/views/comments/terms')
})
},
"{submit} click" : function(el, event) {
event.preventDefault();
// Reset notices
self.resetNotice();
// Add loading indicator on the button
$(el).attr('disabled', true);
var tmp = $(el).html();
$(el).html('<i class="fa fa-repeat fa-spin"></i>');
// Get the form values
var data = self.getValues();
// Perform an ajax call to submit the comment
EasyBlog.ajax('site/views/comments/save', data)
.done(function(output, message, state) {
self.setNotice(message, state);
self.trigger('commentAdded',[output, data]);


})
.fail(function(message) {
self.setNotice(message, 'error');
})
.always(function(){
$(el).removeAttr('disabled');
$(el).html(tmp);
self.trigger('reloadCaptcha');
});
return false;
}
}
});
module.resolve();
});
});

我冒昧地猜测添加location.reload()的位置将在此片段中:

"{self} commentAdded": function()
{
// Increment the counter
var count = self.counter().html();
count = parseInt(count) + 1;
self.counter().html(count.toString());
self.resetForm();
},

所以也许像:

"{self} commentAdded": function()
{
// Increment the counter
var count = self.counter().html();
count = parseInt(count) + 1;
self.counter().html(count.toString());
self.resetForm();
/* reload */
location.reload();
},

或者,您可以将其添加到EasyBlog.ajax%内的.done方法中。不过,通过查看代码,我认为几乎相同

相关内容

最新更新