Requirements
- Display movie datas inside all tags that have the class movie_list
- Make an ajax call for fetching movie datas
- Fill loading text before fetch data
- If any error occurs during loading, the content should be empty
What I did
- Cached all elements which have the class movie_list
- Made functions which have only one responsibility
- Functions take movieList elements for making easy to unit test
What I want to know
- More readable/reusable code
- Any performance improvement point
- Any good design pattern on here
function loadMovies() {
const url = 'http://moviedatas.com/movies';
const movieList = $('.movie_list');
setOnLoading(movieList);
$.ajax({
method: 'GET',
url: url,
data: {
count: 10
},
success: function(response) {
movieList.each(function() {
const wrapper = $(this);
render(wrapper, items);
});
},
error: function(error) {
onError(movieList);
}
})
}
function setOnLoading(movieList) {
movieList.each(function() {
$(this).html('Loading...');
});
}
function render(wrapper, items) {
const template = `<div class="movie_item">
<div class="title">{title}</div>
<div class="desc">{description}</div>
</div>`;
let movies = '';
for (let i = 0; i < items.length; i++) {
movies += template
.replace('{title}', items[i].title)
.replace('{description}', items[i].description);
}
wrapper.html(movies);
}
function onError(movieList) {
movieList.each(function() {
$(this).html('');
});
}
items
inrender(wrapper, items);
? Nor does the code make semantic sense, it reads as creating a movie list of movie lists?? You will have to clarify and fix the code to get a good review. \$\endgroup\$