diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd5106f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_STORE diff --git a/ICanHaz.load.js b/ICanHaz.load.js new file mode 100644 index 0000000..d5735a5 --- /dev/null +++ b/ICanHaz.load.js @@ -0,0 +1,71 @@ +(function(window, document, $) { + + /* + * ICanHaz.load.js + * + * Usage: + * + * ich.load('template', function() { + * // Executed as soon as template loading has finished + * var $tpl = ich.template({ + * foo: 'bar' + * }); + * }); + * + * + * by Finn-Lennart Heemeyer, License: MIT + */ + + var ich = window.ich, + templates = {}; + + ich.load = function(templateName, cb, templatePath, templateExtension) { + if(!templates[templateName]) { + + // Save template loading state und all callbacks here + templates[templateName] = { + ready: false, + callbacks: [cb] + }; + + var path = [(templatePath || ich.templatePath), '/', + templateName, '.', + (templateExtension || ich.templateExtension)].join(''); + + + // Request template file async + $.get(path, function(data) { + + // Add template to use in callbacks + ich.addTemplate(templateName, data); + + // Mark template as loaded + templates[templateName].ready = true; + + // Execute all callbacks + for (var i = 0; i < templates[templateName].callbacks.length; i++) { + templates[templateName].callbacks[i](); + }; + }); + } else if (templates[templateName].ready) { + // If template has been loaded before, execute callback + cb(); + } else { + // If template loading was started, add cb to callbacks + templates[templateName].callbacks.push(cb); + } + }; + + + /* + * Make sure to also clean up templates object when calling clearAll() + */ + var clearAll = ich.clearAll; + + ich.clearAll = function() { + templates = {}; + + clearAll(); + }; + +})(window, window.document, window.jQuery) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b6e456e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Finn-Lennart Heemeyer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/test/index.html b/test/index.html new file mode 100644 index 0000000..f36c1df --- /dev/null +++ b/test/index.html @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + diff --git a/test/templates/template.mustache b/test/templates/template.mustache new file mode 100644 index 0000000..f4d3812 --- /dev/null +++ b/test/templates/template.mustache @@ -0,0 +1,3 @@ + diff --git a/test/tpl/template.mus b/test/tpl/template.mus new file mode 100644 index 0000000..3adc149 --- /dev/null +++ b/test/tpl/template.mus @@ -0,0 +1,3 @@ +