Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: CoffeeScript  |  size: 2.23 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (($) ->
  2.  
  3.   class View
  4.  
  5.     #collection of embedded templates
  6.     @templates:
  7.    
  8.       _form: (args) ->
  9.         inner = args.inner ? []
  10.         $form = $('<form>', action: "${action}", method: "${method}")
  11.         for el in inner
  12.           if el.length
  13.             div = $('<div>')
  14.             div.html el
  15.             $form.append div.html()
  16.         $form.get(0).outerHTML
  17.        
  18.       _simple_element: (element, args) ->
  19.         html = del args, 'html'
  20.         $el = $("<#{element}>")
  21.         $el.html html
  22.         for attr, value of args then $el.attr attr, value
  23.         $el.get(0).outerHTML
  24.        
  25.       _label: (args) -> $.view.templates._simple_element 'label', args
  26.        
  27.       _input: (args) ->
  28.         label = del args, 'label'
  29.         label =
  30.           if label? and args.name?
  31.             """<div class="label">#{ $.view('_label', {'for': args.name, html: label}).get(0).outerHTML }</div>"""
  32.           else
  33.             ''
  34.         $el = $('<input>')
  35.         for attr, value of args then $el.attr attr, value
  36.         """
  37.        <div class="item ${name}">
  38.          #{label}<div class="element">#{ $el.get(0).outerHTML }</div>
  39.        </div>
  40.        """
  41.        
  42.       _select: (args) ->
  43.         options = del args, 'options'
  44.         html_options = ''
  45.         html_options += "<option value=\"#{key}\">#{value}</option>" for key, value of options
  46.         $el = $('<select>')
  47.         for attr, value of args then $el.attr attr, value
  48.         $el.html html_options
  49.         """<div class="item ${name}">#{ $el.get(0).outerHTML }</div>"""
  50.      
  51.      
  52.     constructor: (tmpl, args = {}) ->
  53.       @templates ?= View.templates
  54.       inline = del args, 'inline'
  55.       _args = parse_args inline if inline?
  56.       extend args, _args
  57.       if tmpl[0] is '_'
  58.         try
  59.           out = $.tmpl @templates[tmpl](args), args
  60.         catch e
  61.           log e
  62.       else
  63.         out = $("##{tmpl}").tmpl args
  64.       return out
  65.  
  66.     #refactory
  67.     parse_args = (line = '') ->
  68.       hash = {}
  69.       args = line.split ','
  70.       for tmp in args
  71.         _args = tmp.split '='
  72.         key = _args[0].strip()
  73.         val = _args[1].strip()
  74.         hash[key] = val
  75.       hash
  76.  
  77.  
  78.   $.extend view: View
  79.  
  80. ) jQuery