Skip to content

Instantly share code, notes, and snippets.

@mklabs
Created February 12, 2011 14:23
Show Gist options
  • Select an option

  • Save mklabs/823790 to your computer and use it in GitHub Desktop.

Select an option

Save mklabs/823790 to your computer and use it in GitHub Desktop.
A module that acts as a service to request localy stored markdown file, this version includes a simple cache wrapper
var markdownService =(function(){
var cache = function(fn){
var c = {};
return function(file, cb) {
var item = c[file],
_cb = cb,
cb = function(r) {
var cFile = c[file];
if(cFile) {
cFile.content = r;
}
return _cb.apply(this, arguments);
};
item = item ? item : (c[file] = {
filename: file,
cb: cb,
content: ''
});
if(item.content) {
return cb.call(this, item.content);
}
fn.apply(this, arguments);
}
};
return {
baseUrl: './wiki/',
base: "Home.md",
request: cache(function(file, cb){
return $.ajax({
url: file,
dataType: 'text',
success: $.proxy(function(){
cb.apply(this, arguments);
}, this)
});
}),
get: cache(function(file, cb) {
if($.isFunction(file)) {
cb = file;
file = this.base;
}
file = file === "" ? this.base : file;
return this.request(this.baseUrl + file, cb);
})
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment