Created
October 15, 2010 17:01
-
-
Save AlyxRen/628546 to your computer and use it in GitHub Desktop.
Load a template file from a external source on the same domain. supports passing AJAX data for dynamic templates, as well as caching.
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
(function($){ | |
var cache, defaults; | |
cache = {}; | |
defaults = { | |
//ajax information | |
'url': "", | |
'method': "GET", | |
'ajxData': {}, | |
//template Options | |
'cache': true, | |
'execute': false, | |
'data': false, | |
'callBack': false | |
}; | |
$.extend({ | |
'extTmpl': function(options){ | |
var opt,results; | |
opt = $.extend({},defaults,options); | |
if ( opt.cache && cache[opt.url] + JSON.stringify( opt.ajxData ) ){ | |
results = $.tmpl( cache[opt.url], opt.data ) | |
calback && callback.call( | |
results, | |
results | |
); | |
}else { | |
$.ajax({ | |
url: opt.url, | |
type: opt.method, | |
data: opt.ajxData, | |
success: function(data,success,xhr){ | |
vartmpl, results; | |
tmpl = div | |
.clone() | |
.text(data) | |
.template(); | |
if (opt.cache){ cache[ | |
opt.url + JSON.stringify( opt.ajxData ) | |
] = tmpl; } | |
opt.execute && ( results = $.tmpl( tmpl, opt.data ) ); | |
callback && callback.call( | |
results, //this | |
results | |
); | |
} | |
}); | |
} | |
} | |
}); | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment