Posted By

nerdfiles on 01/14/13


Tagged

javascript js jquery boilerplate CoffeeScript


Versions (?)

Coffeequery (jQuery in Coffeescript)


 / Published in: JavaScript
 

It's a boilerplate for dealing with plugins in Coffeescript, when authoring jQuery style.

  1. $ = jQuery
  2.  
  3. unless Array::filter
  4. Array::filter = (callback) ->
  5. element for element in this when callback element
  6.  
  7. String::capitalize = () ->
  8. (this.split(/\s+/).map (word) -> word[0].toUpperCase() + word[1..-1].toLowerCase()).join ' '
  9.  
  10. (($) ->
  11.  
  12. # USAGE
  13. #
  14. # doc = document
  15. # ($ doc).ready ->
  16. # ($ '.elem').plug 'shy': 'pie'
  17. # (($ '.elem').data 'plug').pub_meth()
  18. # (($ '.elem').data 'plug').options.foo
  19. #
  20. # @nerdfiles 13 2013 01 16:29:02
  21.  
  22. $.plug = (el, opts) ->
  23. @el = el
  24. @$el = $ el
  25. @$el.data "plug", @
  26. @init = =>
  27. @options = $.extend { }, $.plug.def_opts, opts
  28. # return ...
  29. @
  30. # sample public method
  31. @pub_meth = (params) =>
  32. # pass
  33. # sample private method
  34. pvt_meth = (params) =>
  35. # pass
  36. @init()
  37. $.plug.def_opts =
  38. opt1: ''
  39. opt2: ''
  40.  
  41. $.fn.plug = (opts) ->
  42. $.each @, (i, el) ->
  43. $el = $ el
  44.  
  45. unless $el.data 'plug'
  46. $el.data 'plug', new $.plug el, opts
  47.  
  48. )($);

Report this snippet  

You need to login to post a comment.