yii2mod / yii2-comments

Comments module for Yii2
MIT License
158 stars 63 forks source link

Add js events #66

Closed ihorchepurnyi closed 7 years ago

ihorchepurnyi commented 7 years ago

Create the following list of events:

ihorchepurnyi commented 7 years ago

Hi, please update your package to dev-master version

ihorchepurnyi commented 7 years ago

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'
);
?>
carlosgarcia8 commented 7 years ago

Hi, thanks for the update, gonna work on it!

carlosgarcia8 commented 7 years ago

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.

ihorchepurnyi commented 7 years ago

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'
);
?>
carlosgarcia8 commented 7 years ago

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?

ihorchepurnyi commented 7 years ago

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.

ihorchepurnyi commented 7 years ago

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');
        }
carlosgarcia8 commented 7 years ago

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.

ihorchepurnyi commented 7 years ago

You're welcome!