forked from markdalgleish/tmpload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmpload.js
47 lines (45 loc) · 1.57 KB
/
tmpload.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
/*
tmpload jQuery Plugin v1.1
Copyright 2011, Mark Dalgleish
This content is released under the MIT License
github.com/markdalgleish/tmpload/blob/master/MIT-LICENSE.txt
*/
(function($, undefined) {
//Template cache
var templates = {};
$.tmpload = function(obj, url) {
if (url === undefined) {
//Declaring template(s)
if (typeof obj === "object") {
if (obj.length) {
//Array of declaration objects
for (var i = 0; i < obj.length; i++) {
templates[obj[i].name] = obj[i].url;
}
} else {
//A single declaration object
templates[obj.name] = obj.url;
}
}
//Loading the template
else if (typeof obj === "string") {
if (typeof templates[obj] === "string") {
//The template hasn't been loaded yet
return templates[obj] = $.Deferred(function(dfd) {
$.get(templates[obj]).success(function(d) {
dfd.resolve($.template(obj, d));
}).error(function(d) {
dfd.reject(d);
});
}).promise();
} else {
//The template has already been cached
return templates[obj];
}
}
//Declaring a single template
} else {
templates[obj] = url;
}
};
})(jQuery);