Pankaj Berde | c4ae4b8 | 2013-01-12 09:56:47 -0800 | [diff] [blame] | 1 | // template loader from Christophe Coenraets |
| 2 | tpl = { |
| 3 | |
| 4 | // Hash of preloaded templates for the app |
| 5 | templates:{}, |
| 6 | |
| 7 | // Recursively pre-load all the templates for the app. |
| 8 | // This implementation should be changed in a production environment. All the template files should be |
| 9 | // concatenated in a single file. |
| 10 | loadTemplates:function (names, callback) { |
| 11 | |
| 12 | var that = this; |
| 13 | |
| 14 | var loadTemplate = function (index) { |
| 15 | var name = names[index]; |
| 16 | console.log('Loading template: ' + name); |
| 17 | $.get('tpl/' + name + '.html', function (data) { |
| 18 | that.templates[name] = data; |
| 19 | index++; |
| 20 | if (index < names.length) { |
| 21 | loadTemplate(index); |
| 22 | } else { |
| 23 | callback(); |
| 24 | } |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | loadTemplate(0); |
| 29 | }, |
| 30 | |
| 31 | // Get template by name from hash of preloaded templates |
| 32 | get:function (name) { |
| 33 | return this.templates[name]; |
| 34 | } |
| 35 | |
| 36 | }; |