Closed 3point14guy closed 7 years ago
So you're correct in that you need to attach a click handler after an element has been rendered, but the way you have it set up now does not actually wait for api.requestAllMovies()
to finish running before adding that handler, because it is not part of a .then
statement. I would try putting this code inside ui.allMoviesSuccess
const getAllMovies = function (event) {
event.preventDefault()
$('#delete-movie').on('click', deleteMovie)
console.log('events getAllMovies')
api.requestAllMovies()
- .then(ui.allMoviesSuccess)
- .catch(ui.allMovieFailure)
- $('document').on('click', '#delete-movie', deleteMovie)
+ .then(ui.allMoviesSuccess)
+ .then(() => $('document').on('click', '#delete-movie', deleteMovie))
+ .catch(ui.allMovieFailure)
}
Or put it in the first .then()
block right after ui.allMoviesSuccess
Ben, I had tried putting it in the ui previously, but then it looked like I was going to have a circular reference problem bc the deleteMovie function was in my events file.
Chris, I tried your suggestion and the click event is still not triggering.
const getAllMovies = function (event) {
event.preventDefault()
$('#delete-movie').on('click', deleteMovie)
console.log('events getAllMovies')
api.requestAllMovies()
.then(ui.allMoviesSuccess)
.then(() => $('document').on('click', '#delete-movie', deleteMovie))
.catch(ui.allMovieFailure)
}
Moved the listener event to the ui and now the click event now works. However, I do have the circular dependency. I will open a new issue for that.
const allMoviesSuccess = function (data) {
store.movie = data.movie
console.log('get all Movies success')
$('.display-list').empty()
const displayMoviesHTML = displayMoviesTemplate({ movies: data.movies })
$('.display-list').append(displayMoviesHTML)
$('#delete-movie').on('click', deleteMovie)
}
I created a button in handlebars that I would like to send an AJAX request. I believe I have the event listener moved to the correct place so that it will 'listen' after the button is created, but somehow, the AJAX request still is not being triggered.
This is similar to issue #985, which I have implemented that fix to the best of my ability.
I would appreciate assistance troubleshooting this.