@@ -2,23 +2,22 @@ module.exports = function (Twig) {
2
2
'use strict' ;
3
3
4
4
Twig . Templates . registerLoader ( 'ajax' , function ( location , params , callback , errorCallback ) {
5
- var template ,
6
- xmlhttp ,
7
- precompiled = params . precompiled ,
8
- parser = this . parsers [ params . parser ] || this . parser . twig ;
5
+ let template ;
6
+ const { precompiled} = params ;
7
+ const parser = this . parsers [ params . parser ] || this . parser . twig ;
9
8
10
- if ( typeof XMLHttpRequest === " undefined" ) {
9
+ if ( typeof XMLHttpRequest === ' undefined' ) {
11
10
throw new Twig . Error ( 'Unsupported platform: Unable to do ajax requests ' +
12
11
'because there is no "XMLHTTPRequest" implementation' ) ;
13
12
}
14
13
15
- xmlhttp = new XMLHttpRequest ( ) ;
16
- xmlhttp . onreadystatechange = function ( ) {
17
- var data = null ;
14
+ const xmlhttp = new XMLHttpRequest ( ) ;
15
+ xmlhttp . onreadystatechange = function ( ) {
16
+ let data = null ;
18
17
19
- if ( xmlhttp . readyState === 4 ) {
20
- if ( xmlhttp . status === 200 || ( window . cordova && xmlhttp . status == 0 ) ) {
21
- Twig . log . debug ( " Got template " , xmlhttp . responseText ) ;
18
+ if ( xmlhttp . readyState === 4 ) {
19
+ if ( xmlhttp . status === 200 || ( window . cordova && xmlhttp . status === 0 ) ) {
20
+ Twig . log . debug ( ' Got template ' , xmlhttp . responseText ) ;
22
21
23
22
if ( precompiled === true ) {
24
23
data = JSON . parse ( xmlhttp . responseText ) ;
@@ -34,22 +33,20 @@ module.exports = function (Twig) {
34
33
if ( typeof callback === 'function' ) {
35
34
callback ( template ) ;
36
35
}
37
- } else {
38
- if ( typeof errorCallback === 'function' ) {
39
- errorCallback ( xmlhttp ) ;
40
- }
36
+ } else if ( typeof errorCallback === 'function' ) {
37
+ errorCallback ( xmlhttp ) ;
41
38
}
42
39
}
43
40
} ;
44
- xmlhttp . open ( "GET" , location , ! ! params . async ) ;
41
+
42
+ xmlhttp . open ( 'GET' , location , Boolean ( params . async ) ) ;
45
43
xmlhttp . send ( ) ;
46
44
47
45
if ( params . async ) {
48
46
// TODO: return deferred promise
49
47
return true ;
50
- } else {
51
- return template ;
52
48
}
53
- } ) ;
54
49
50
+ return template ;
51
+ } ) ;
55
52
} ;
0 commit comments