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

后端 未结 8 1155
温柔的废话
温柔的废话 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:32

    The easiest way is to extend $.fn object

    Simple jQuery plugin can be written in CoffeeScript as follows:

    $.extend $.fn,
    
      disable: ->
        @each ->
          e = $(this)
          e.attr("disabled", "disabled") if e.is("button") or e.is("input")
    

    it will compile to

    (function() {
      $.extend($.fn, {
        disable: function() {
          return this.each(function() {
            var e;
            e = $(this);
            if (e.is("button") || e.is("input")) {
              return e.attr("disabled", "disabled");
            }
          });
        }
      });
    }).call(this);
    

提交回复
热议问题