在coffeescript中编写一个jquery插件 - 如何获取“(function($)”和“(jQuery)”? [英] Writing a jquery plugin in coffeescript - how to get "(function($)" and "(jQuery)"?

查看:106
本文介绍了在coffeescript中编写一个jquery插件 - 如何获取“(function($)”和“(jQuery)”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的coffeescript以这个开始:

$

我在coffeescript中编写一个jquery插件,但不知道如何获得函数包装器。 b
$ b

  $。fn.extend({
myplugin: - >
@each - >

这将使用函数包装器创建javascript:

 (function(){
$ .fn.extend({
myplugin:function(){
return this.each(function(){

但我想要一个'$'像这样传递:

 (function($){
$ .fn.extend({

类似于结尾我有...没有什么特别的coffeescript。

我在javascript中得到这个:



<$ p $但是希望这样:

>

 })(jQuery); 


b $ b

有没有人知道如何使用coffeescript编译器来实现这一点?
或者什么是在coffeescript中完成这个操作的最好方法?

解决方案

答案是,你不需要像CoffeeScript中那样调用它 - 你的脚本已经安全地包装在一个闭包中,所以没有必要jQuery-passed-in-as参数技巧。只要写:

  $ = jQuery 

...在脚本的顶部,你很好。


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.extend({
    myplugin: ->
        @each ->

Which creates the javascript with a function wrapper:

(function() {
  $.fn.extend({
      myplugin: function() {
          return this.each(function() {

but I want a '$' passed in like this:

(function($) {
  $.fn.extend({

Similar for the ending I have... nothing in particular in coffeescript.
I get this in javascript:

})();

But would like this:

})(jQuery);

Does anyone know how to achieve this with the coffeescript compiler? Or what is the best way to get this done within coffeescript?

解决方案

The answer is that you don't need to call it like that in CoffeeScript -- your script is already safely wrapped in a closure, so there's no need for jQuery-passed-in-as-a-parameter-tricks. Just write:

$ = jQuery

... at the top of your script, and you're good to go.

这篇关于在coffeescript中编写一个jquery插件 - 如何获取“(function($)”和“(jQuery)”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆