futur1st
futur1st

Reputation: 61

Where is the error: Java Script code

Aplication is built on Yii2 with using Kartik's star-rating widget. Js code have an error:

Image with error in console:

Uncaught SyntaxError: Unexpected token (

Code:

    <?php 
    $js = <<<JS
                            function (event, value, caption) { 
                                $.ajax({
                                    type: 'GET', 
                                    url: '/rating/update-rating', 
                                    data: { 
                                        points: value, 
                                        post_id: $post->id
                                    }, 
                                    success: function(res) { 
                                        $(event.currentTarget).rating('update', res); 
                                    }, 
                                    error: function(e) {  
                                        console.log(e); 
                                    }  
                                }); 
                            }
    JS;
    $this->registerJs($js);

echo StarRating::widget([
                        'name' => $post->post_rate,
                        'value' => isset($post->rating[0]['dec_avg']) ? $post->rating[0]['dec_avg'] : 0,
                        'pluginOptions' => [ ... ] 
                            'pluginEvents' => [
                                'rating:change' => $js,
                            ],

Upvotes: 0

Views: 327

Answers (1)

Bahadir Tasdemir
Bahadir Tasdemir

Reputation: 10803

You forgot to add a name for your js function:

function functionName(event, value, caption) {
    $.ajax({
        type: 'GET',
        url: '/rating/update-rating',
        data: {
            points: value,
            post_id: $post - > id
        },
        success: function(res) {
            $(event.currentTarget).rating('update', res);
        },
        error: function(e) {
            console.log(e);
        }
    });
}

Upvotes: 2

Related Questions