Skip to content

Commit 20041d1

Browse files
committed
requests to tmdb api async by fetch
1 parent f2b37f5 commit 20041d1

File tree

3 files changed

+23
-87
lines changed

3 files changed

+23
-87
lines changed

src/app/lib/views/item.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
}
170170
},
171171

172-
loadImage: function () {
172+
loadImage: async function () {
173173
var noimg = 'images/posterholder.png';
174174
var poster = this.model.get('image');
175175
if (!poster && this.model.get('images') && this.model.get('images').poster){
@@ -178,22 +178,9 @@
178178
poster = this.model.get('poster');
179179
} else {
180180
var imdb = this.model.get('imdb_id'),
181-
api_key = Settings.tmdb.api_key,
182-
movie = (function () {
183-
var tmp = null;
184-
$.ajax({
185-
url: 'http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&append_to_response=videos',
186-
type: 'get',
187-
dataType: 'json',
188-
timeout: 5000,
189-
async: false,
190-
global: false,
191-
success: function (data) {
192-
tmp = data;
193-
}
194-
});
195-
return tmp;
196-
}());
181+
api_key = Settings.tmdb.api_key;
182+
const response = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&append_to_response=videos');
183+
const movie = await response.json();
197184
poster = movie && movie.poster_path ? 'http://image.tmdb.org/t/p/w500' + movie.poster_path : noimg;
198185
this.model.set('poster', poster);
199186
!this.model.get('synopsis') && movie && movie.overview ? this.model.set('synopsis', movie.overview) : null;

src/app/lib/views/movie_detail.js

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@
125125
this.initKeyboardShortcuts();
126126
healthButton.render();
127127

128-
if (curSynopsis.vstatus !== null && curSynopsis.cast === '') {
129-
this.showCast();
130-
}
131-
132128
$('[data-toggle="tooltip"]').tooltip({
133129
html: true
134130
});
@@ -214,26 +210,14 @@
214210
}
215211
},
216212

217-
getMetaData: function () {
213+
getMetaData: async function () {
218214
curSynopsis.vstatus = false;
219215
var imdb = this.model.get('imdb_id'),
220-
api_key = Settings.tmdb.api_key,
221-
lang = Settings.language,
222-
movie = (function () {
223-
var tmp = null;
224-
$.ajax({
225-
url: 'http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&language=' + lang + '&append_to_response=videos,credits',
226-
type: 'get',
227-
dataType: 'json',
228-
timeout: 5000,
229-
async: false,
230-
global: false,
231-
success: function (data) {
232-
tmp = data;
233-
}
234-
});
235-
return tmp;
236-
}());
216+
api_key = Settings.tmdb.api_key,
217+
lang = Settings.language;
218+
const response = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&language=' + lang + '&append_to_response=videos,credits');
219+
const movie = await response.json();
220+
237221
(!this.model.get('synopsis') || (Settings.translateSynopsis && Settings.language !== 'en')) && movie && movie.overview ? this.model.set('synopsis', movie.overview) : null;
238222
(!this.model.get('rating') || this.model.get('rating') === '0' || this.model.get('rating') === '0.0') && movie && movie.vote_average ? this.model.set('rating', movie.vote_average) : null;
239223
(!this.model.get('runtime') || this.model.get('runtime') === '0') && movie && movie.runtime ? this.model.set('runtime', movie.runtime) : null;
@@ -249,22 +233,13 @@
249233
}
250234
// Fallback to english when source and TMDb call in default language that is other than english fail to fetch synopsis
251235
if (!this.model.get('synopsis') && Settings.language !== 'en') {
252-
movie = (function () {
253-
var tmp = null;
254-
$.ajax({
255-
url: 'http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key,
256-
type: 'get',
257-
dataType: 'json',
258-
timeout: 5000,
259-
async: false,
260-
global: false,
261-
success: function (data) {
262-
tmp = data;
263-
}
264-
});
265-
return tmp;
266-
}());
267-
movie && movie.overview ? this.model.set('synopsis', movie.overview) : null;
236+
const responseEn = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key);
237+
const movieEn = await responseEn.json();
238+
movieEn && movieEn.overview ? this.model.set('synopsis', movieEn.overview) : null;
239+
}
240+
241+
if (curSynopsis.vstatus === false && curSynopsis.cast === '') {
242+
this.showCast();
268243
}
269244
},
270245

@@ -400,27 +375,14 @@
400375
}
401376
},
402377

403-
openTmdb: function(e) {
378+
openTmdb: async function(e) {
404379
let imdb = this.model.get('imdb_id'),
405380
tmdb = this.model.get('tmdb_id'),
406381
api_key = Settings.tmdb.api_key;
407382

408383
if (!tmdb && !this.model.get('getmetarunned')) {
409-
let movie = (function () {
410-
let tmp = null;
411-
$.ajax({
412-
url: 'http://api.themoviedb.org/3/find/' + imdb + '?api_key=' + api_key + '&external_source=imdb_id',
413-
type: 'get',
414-
dataType: 'json',
415-
timeout: 5000,
416-
async: false,
417-
global: false,
418-
success: function (data) {
419-
tmp = data;
420-
}
421-
});
422-
return tmp;
423-
}());
384+
const response = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&external_source=imdb_id');
385+
const movie = await response.json();
424386
movie && movie.movie_results && movie.movie_results[0] && movie.movie_results[0].id ? this.model.set('tmdb_id', movie.movie_results[0].id) : null;
425387
tmdb = this.model.get('tmdb_id');
426388
}

src/app/lib/views/show_detail.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -417,27 +417,14 @@
417417
}
418418
},
419419

420-
openTmdb: function(e) {
420+
openTmdb: async function(e) {
421421
let imdb = this.model.get('imdb_id'),
422422
tmdb = this.model.get('tmdb_id'),
423423
api_key = Settings.tmdb.api_key;
424424

425425
if (!tmdb) {
426-
let show = (function () {
427-
let tmp = null;
428-
$.ajax({
429-
url: 'http://api.themoviedb.org/3/find/' + imdb + '?api_key=' + api_key + '&external_source=imdb_id',
430-
type: 'get',
431-
dataType: 'json',
432-
timeout: 5000,
433-
async: false,
434-
global: false,
435-
success: function (data) {
436-
tmp = data;
437-
}
438-
});
439-
return tmp;
440-
}());
426+
const response = await fetch('http://api.themoviedb.org/3/movie/' + imdb + '?api_key=' + api_key + '&external_source=imdb_id');
427+
const show = await response.json();
441428
show && show.tv_results && show.tv_results[0] && show.tv_results[0].id ? this.model.set('tmdb_id', show.tv_results[0].id) : null;
442429
tmdb = this.model.get('tmdb_id');
443430
}

0 commit comments

Comments
 (0)