-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.tmpldeck.coffee
52 lines (37 loc) · 1013 Bytes
/
jquery.tmpldeck.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
do ($ = jQuery) -> # encapsulate plugin
# main
class TmplDeck
_options:
dataType: 'text'
url: null
cache: true
constructor: (url, options) ->
# handle instance creation wo new
if not (@ instanceof arguments.callee)
return new TmplDeck url, options
@_options = $.extend {}, @_options,
url: url
@_fetchDefer = $.Deferred()
@_cache = {}
load: ->
($.ajax @_options).then (data) =>
@_fetchedText = data
@_fetchDefer.resolve()
ready: (fn) ->
@_fetchDefer.done -> fn()
@
draw: (id) ->
cached = @_cache[id]
if cached then return cached
re = new RegExp "(^|\\n)#{id}\\s\{\{\{\{\\r?\\n([\\s\\S]+?)\}\}\}\}"
res = @_fetchedText.match(re)
if res
return (@_cache[id] = res[2])
else
return null
# underscore.js enhancement
TmplDeck::tmpl = (id, data) ->
(_.template @draw(id)) data
# globalify
#
$.TmplDeck = TmplDeck