Writing a jquery plugin in coffeescript - how to get “(function($)” and “(jQuery)”?

后端 未结 8 1131
温柔的废话
温柔的废话 2020-12-13 12:55

I am writing a jquery plugin in coffeescript but am not sure how to get the function wrapper part right.

My coffeescript starts with this:

$.fn.exten         


        
相关标签:
8条回答
  • 2020-12-13 13:49

    Simple and Straightforward

    This is all I had to do in order to add my own method, cleanFadeIn, on jQuery objects. It returns the objects for chaining as well:

    $.fn.extend
      cleanFadeIn: ->                     # $('.notice').cleanFadeIn
        return $(@).each ->               # returns the objects for easy chaining.
          $(@).slideDown 'slow', ->
            $(@).fadeTo 'slow', 1
    
    0 讨论(0)
  • 2020-12-13 13:51

    Although this post is old I found it useful. Here is the coffee-script code that works for me.

    $ -> 
        $('.my-class').hello()
    
    $.fn.hello=-> 
        @each -> 
            $(@).append $ '<div>Hello</div>'
    

    Note: You don't need to declare the $ variable, you can just use it right out of the box.

    0 讨论(0)
提交回复
热议问题