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

后端 未结 8 1156
温柔的废话
温柔的废话 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条回答
  •  旧时难觅i
    2020-12-13 13:36

    UPDATE/EDIT: Yep, as per Jeremy's explanation:

    $ = jQuery
    
    $.fn.myPlugin = () ->
      console.log('test fired')
    

    compiles to:

    (function() {
      var $;
      $ = jQuery;
      $.fn.myPlugin = function() {
        return console.log('test fired');
      };
    }).call(this);
    

    Which works just fine as a jQuery plugin: $('body').myPlugin();

    Original:

    Okay, i think I may getting close on this one, let me know if it helps.

    (($) ->
      $.fn.extend =
        myplugin: ->
        @each: ->
    )(jQuery)
    

    renders into:

    (function() {
      (function($) {
        return $.fn.extend = {
          myplugin: function() {},
          this.each: function() {}
        };
      })(jQuery);
    }).call(this);
    

提交回复
热议问题