Let say I have function to get templates, if template was never before use it should send request by AJAX and after that return template, if template was used it should be return from cache.
Sample code:
var getTemplate = (function(jQuery){
//full template obj = {url: '', html: ''}
var templates = {test: {url: '/templates/test.html'}};
function getTemplate(templateId){
if(templates[templateId].html){
return templates[templateId];
}
jQuery.ajax({
method: "get",
url: templates[templateId].url
}).success(function(respond){
templates[templateId].html = respond;
});
//no idea what next...
return getTemplate
}
})(jQuery);
Sample use:
var template = getTemplate('test') should always return /templates/test.html content
I don't wont use async: false and any framework. I wont learn how to do that ;)
templates[templateId]after ajax endreturntemplate data. The only way to get the data (at least in a feasible way) would be to pass a callback function.