annotate src/Kachkaev/JstmplBundle/Resources/assets/js/tmpl.js @ 1:f38015048f48 tip

Added GPL
author Daniel Wolff
date Sat, 13 Feb 2016 20:43:38 +0100
parents 493bcb69166c
children
rev   line source
Daniel@0 1 tmpl = (function() {
Daniel@0 2
Daniel@0 3 var tmpl = {};
Daniel@0 4
Daniel@0 5 tmpl.autoload = [];
Daniel@0 6
Daniel@0 7 /**
Daniel@0 8 * Renders template from a preloaded collection or current page
Daniel@0 9 * @param template template id, e.g. "calendar:flash_error-load"
Daniel@0 10 * @returns jQuery object
Daniel@0 11 */
Daniel@0 12 tmpl.render = function(template, data, options) {
Daniel@0 13 //Loading a preloaded template
Daniel@0 14 $result = $.tmpl(template, data, options);
Daniel@0 15 // Use of a preloaded template failed. Getting template from current page (<script id="...">)
Daniel@0 16 if ($result.text().indexOf(template) == 0) {
Daniel@0 17 $result = $.tmpl($("#"+template.replace(':','\\:')).html(), data, options);
Daniel@0 18 };
Daniel@0 19 // Both cases failed. Will return an empty object.
Daniel@0 20 if (!$result.text || $result.text().indexOf(template) == 0) {
Daniel@0 21 console.warn('Could not find template '+template);
Daniel@0 22 $result = $();
Daniel@0 23 }
Daniel@0 24
Daniel@0 25 return $result;
Daniel@0 26 };
Daniel@0 27
Daniel@0 28 tmpl.loadCollection = function(collectionName) {
Daniel@0 29 $.ajax({
Daniel@0 30 url : '/tmpl/' + collectionName + '.html',
Daniel@0 31 success : function(result) {
Daniel@0 32 $(result).each(function(i, t) {
Daniel@0 33 var $t = $(t);
Daniel@0 34 var id = $t.attr('id');
Daniel@0 35 if (id) {
Daniel@0 36 $.template(id, $t.html());
Daniel@0 37 }
Daniel@0 38 });
Daniel@0 39 },
Daniel@0 40 async : false
Daniel@0 41 });
Daniel@0 42 };
Daniel@0 43
Daniel@0 44 $(function() {
Daniel@0 45 $.each(tmpl.autoload, function(i, collectionName){
Daniel@0 46 tmpl.loadCollection(collectionName);
Daniel@0 47 });
Daniel@0 48 });
Daniel@0 49
Daniel@0 50 return tmpl;
Daniel@0 51 })();