forked from cbourgois/HandlebarsLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandlebarLoader.js
111 lines (98 loc) · 2.26 KB
/
HandlebarLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// HandlebarLoader.js 0.1
// (c) 2012 Cyrille Bourgois
// HandlebarLoader may be freely distributed under the MIT license.
// https://github.com/cbourgois/HandlebarLoader
HandlebarLoader = function( options )
{
this._options = _.extend(
{
'baseUrl': 'tpl/',
'partialUrl': 'tpl/partials/',
'extension': 'html'
},
options
);
this._tpl = {};
this._templates = [];
this._partials = [];
this._callback = undefined;
};
_.extend( HandlebarLoader.prototype, {
loadTemplate : function ( index )
{
var name = this._templates[ index ];
var that = this;
$.ajax( {
url: that._options.baseUrl + name + '.' + that._options.extension,
success: function (data)
{
that._tpl[name] = function( params )
{
var tpl = Handlebars.compile( data );
return tpl(params);
};
index++;
if ( index < that._templates.length )
{
that.loadTemplate( index );
}
else if ( that._partials.length > 0 )
{
that.loadPartial( 0 );
}
else if ( that._callback )
{
that._callback();
}
},
dataType: 'html'
});
},
loadPartial: function ( index )
{
var name = this._partials[ index ];
var that = this;
$.ajax( {
url: that._options.partialUrl + name + '.' + that._options.extension,
success: function ( data )
{
Handlebars.registerPartial( name, data );
index++;
if ( index < that._partials.length )
{
loadPartial(index);
}
else if ( that._callback )
{
that._callback();
}
},
dataType: 'html'
});
},
load: function ( nameTemplates, namePartials, callback ) {
this._templates = nameTemplates;
this._partials = namePartials;
this._callback = callback;
if ( this._templates.length > 0 )
{
this.loadTemplate( 0 );
}
else if ( this._partials.length > 0 )
{
this.loadPartial( 0 );
}
else if ( this._callback )
{
this._callback();
}
},
getTemplate :function( name )
{
return this._tpl[ name ];
},
getAllTemplates: function()
{
return this._tpl;
}
});