
Untitled
By: a guest on
May 8th, 2012 | syntax:
CoffeeScript | size: 2.23 KB | hits: 26 | expires: Never
(($) ->
class View
#collection of embedded templates
@templates:
_form: (args) ->
inner = args.inner ? []
$form = $('<form>', action: "${action}", method: "${method}")
for el in inner
if el.length
div = $('<div>')
div.html el
$form.append div.html()
$form.get(0).outerHTML
_simple_element: (element, args) ->
html = del args, 'html'
$el = $("<#{element}>")
$el.html html
for attr, value of args then $el.attr attr, value
$el.get(0).outerHTML
_label: (args) -> $.view.templates._simple_element 'label', args
_input: (args) ->
label = del args, 'label'
label =
if label? and args.name?
"""<div class="label">#{ $.view('_label', {'for': args.name, html: label}).get(0).outerHTML }</div>"""
else
''
$el = $('<input>')
for attr, value of args then $el.attr attr, value
"""
<div class="item ${name}">
#{label}<div class="element">#{ $el.get(0).outerHTML }</div>
</div>
"""
_select: (args) ->
options = del args, 'options'
html_options = ''
html_options += "<option value=\"#{key}\">#{value}</option>" for key, value of options
$el = $('<select>')
for attr, value of args then $el.attr attr, value
$el.html html_options
"""<div class="item ${name}">#{ $el.get(0).outerHTML }</div>"""
constructor: (tmpl, args = {}) ->
@templates ?= View.templates
inline = del args, 'inline'
_args = parse_args inline if inline?
extend args, _args
if tmpl[0] is '_'
try
out = $.tmpl @templates[tmpl](args), args
catch e
log e
else
out = $("##{tmpl}").tmpl args
return out
#refactory
parse_args = (line = '') ->
hash = {}
args = line.split ','
for tmp in args
_args = tmp.split '='
key = _args[0].strip()
val = _args[1].strip()
hash[key] = val
hash
$.extend view: View
) jQuery