Created
July 28, 2014 08:00
-
-
Save katopz/d88ae24e6d903447ef62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.mixin({templateFromUrl: function (url, data, settings) { | |
var templateHtml = ""; | |
this.cache = this.cache || {}; | |
if (this.cache[url]) { | |
templateHtml = this.cache[url]; | |
} else { | |
$.ajax({ | |
url: url, | |
method: "GET", | |
async: false, | |
success: function(data) { | |
templateHtml = data; | |
} | |
}); | |
this.cache[url] = templateHtml; | |
} | |
return _.template(templateHtml, data, settings); | |
}}); | |
// example | |
var someHtml = _.templateFromUrl("http://example.com/template.html", {"var": "value"}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i used the code and implemented as per my requirement like this.
renderTemplate: function(url, renderid, data){
var compiledTmplate = _.templateFromUrl(url);
$(renderid).html( compiledTmplate( data ) );
}
but when i am using this code. it always return Uncaught ReferenceError: response is not defined whether i am using in ajax success.
like this.
$.ajax({
url: 'getdatafromserver',
type: 'GET',
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function(res){
var datares = res;
renderTemplate("externaltemplate.html", '#renderid', datares);
}
});
Here it is return var datares = res;
datares undefined..
if you write like this window.datares it doesn't give any error and working fine... but i can't set all the variable in window or global scope.
Please help if you know the solution