0

I am following this jQuery tutorial and trying to replicate this inside my laravel project. I cannot call the getMovie() function defined inside movie-info.js to work when movie.blade.php view is loaded. The routes are properly assigned, and I do not get any error but the console print in getMovie() is not accessed. What am I doing wrong?

I used ziggy library to call the named routes.

app.blade.php

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Movie Info</title>

    @routes
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootswatch/4.5.2/cyborg/bootstrap.min.css"
        integrity="sha384-nEnU7Ae+3lD52AK+RGNzgieBWMnEfgTbRHIwEvp1XXPdqdO6uLTd/NwXbzboqjc2" crossorigin="anonymous">
    <link rel="stylesheet" href="{{ asset('css/dist/movie-info.css') }} ">
</head>

<body>
    <nav class="navbar navbar-default>">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="index.html">MovieInfo</a>
            </div>
        </div>
    </nav>

    <div class="container">
        <div class="jumbotron">
            <h3 class="text-center">Search for any movie</h3>
            <form id="searchForm">
                <input type="text" class="form-control" id="searchText" placeholder="Search movie">
            </form>
        </div>
    </div>

    <div class="container">
        @yield('content')
    </div>

    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

    <script src="{{ asset('js/dist/vendor.js') }}"></script>
    <script src="{{ asset('js/dist/manifest.js') }}"></script>
    <script src="{{ asset('js/dist/movie-info.js') }}">
        @yield('slug')
    </script>

</body>

</html>

index.blade.php

@extends('layouts.app')

@section('content')
    <div id="movies" class="row"></div>
@endsection

movie.blade.php

@extends('layouts.app')

@section('content')
    <div id="movie" class="row"></div>
@endsection

@section('slug')
    getMovie();
@endsection

movie-info.js

    import $ from 'jquery';

$(() => {
    $('#searchForm').on('submit', (e) => {
        let searchText = $('#searchText').val();
        console.log(searchText);
        getMovies(searchText);
        e.preventDefault();
    });

    $('#movies').on('click', '.movie-details', (e) => {
        const elementID = e.target.id;
        const imdbid = $('#' + elementID).data('imdbid'); //custom attribute
        console.log(elementID, imdbid);
        movieSelected(imdbid);
    });

});

function getMovies(searchText) {
 
    axios.get('http://www.omdbapi.com?s=' + searchText + '&apikey=thewdb')
        .then((response) => {
            console.log(response);
            let movies = response.data.Search;
            let output = "";
            $.each(movies, (index, movie) => {
                output += `
                    <div class="col-md-3">
                        <div class="well text-center">
                        <img src="${movie.Poster}">
                        <h5>${movie.Title}</h5>   
                        <a data-imdbid='${movie.imdbID}' class="btn btn-primary movie-details" id="movie_${movie.imdbID}" href="#">Movie Details</a>
                        </div>
                    </div>
                `;
            });

            $('#movies').html(output);
        })
        .catch((err) => {
            console.log(err);
        });
}

function movieSelected(id) {
    sessionStorage.setItem('movieId', id); //pass data from one page to another
    window.location.href = route('show-movie-details');
    console.log('my route', route('show-movie-details'));
    // getMovie();
    return false;
}

function getMovie() {
    console.log("in get movie");
}
afsara_ben
  • 542
  • 1
  • 11
  • 30

1 Answers1

0

It's because you call getMovie() inside a script tag with src attribute. Just move it to another script tag without src then it will work as expected. Something like:

...
<script src="{{ asset('js/dist/movie-info.js') }}"></script>
<script>
  @yield('slug')
</script>
...

You can get more info here: What does a script-Tag with src AND content mean?.

Cuong Le Ngoc
  • 11,595
  • 2
  • 17
  • 39