Created
February 12, 2011 14:23
-
-
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
This file contains hidden or 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
| 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