Closed ihorchepurnyi closed 7 years ago
Hi, please update your package to dev-master
version
Now you can use events as follows https://github.com/yii2mod/yii2-comments#using-comment-plugin-events
Basic example:
// in your view file
$this->registerJs(
"
$(document).on('beforeCreate', '#comment-form', function (e) {
console.log(e);
});
$(document).on('afterCreate', '#comment-form', function (e) {
console.log(e);
});
",
\yii\web\View::POS_READY,
'custom-js'
);
?>
Hi, thanks for the update, gonna work on it!
That basic example should be in the view file that uses the Comment::widget or in the comments view like index or _form? I tried that basic example in the one with the widget and its not working right now, gonna keep trying to find out whats going on.
Yes, you're right. I placed this code in the view file, for example:
// your-view.php
<h1>Some page </h1>
<?php echo \yii2mod\comments\widgets\Comment::widget([
'model' => $model,
]); ?>
<?php
$this->registerJs(
"
$(document).on('beforeCreate', '#comment-form', function (e) {
console.log(e);
});
$(document).on('afterCreate', '#comment-form', function (e) {
console.log(e);
});
",
\yii\web\View::POS_READY,
'custom-js'
);
?>
I finally get it working, the assets were not updating the changes. But I'm seeing a problem, are you sure the 'afterCreate' can add events to the comments like, for example:
$(document).on('afterCreate', '#comment-form', function (e) {
console.log('Hi');
$('.comment-author-avatar').on('click', function() {
console.log('Bye');
}
});
This code doesnt add the event on click on the image when the comment is created, it says 'Hi' but the event is not working. Maybe because this code is running when the nodes arent still created?
I think in this situation you need to use click
event on avatar image separate from afterCreate
event. afterCreate
event runs after pjax
has been updated comments form with comments tree.
I think you can use the following code for handle the click on avatar image
$(document).on('click', '.comment-author-avatar', function() {
console.log('Bye');
}
Yes, after a few tries I have finally achieved what I wanted, had a few problems while trying to fill the textarea with the username like '@user213 ' when it is a reply and delete it when you cancel it, but I've found the way. Thank you for your answers and the help.
You're welcome!
Create the following list of events: