2012-07-15 102 views
2

我创建了一个externs文件,可以使用Google Closure编译器的ADVANCED_OPTIMIZATIONS编译jQuery Star Rating Plugin fyneworks.com/jquery/star-rating/#tab-Testing。extern for jQuery Star Rating Plugin和Google Closure Compiler

但是,即使我引用了标准的jQuery extern,'$'也会被重命名,导致插件崩溃。

也许相关:如果我使用未修改的插件,'评分'也会被重命名。我可以修复与部分:

$.fn['rating'] = function(opts) { 

google closure compile jQuery Plugin ......但是,这并不固定“$”(它会是不错的使用未修改的插件,如果可能的话)。

从我在一个外部的企图(这可能是错误的和/或不完整):

// ??? for '$' 

// this one does NOT prevent 'rating' from being renamed 
function rating(arg1) {} 

// the following seem to work: they prevent the functions from being renamed 
rating.focus = function() {} 
rating.blur = function() {} 
rating.fill = function() {} 
... etc. 

命令行(和rating.sh在下载):

java -jar ../compiler-latest/compiler.jar --formatting pretty_print --compilation_level ADVANCED_OPTIMIZATIONS --externs externs/jquery-1.7.js --externs externs/jquery.rating-extern.js --js original/jquery.rating.js --js_output_file jquery.rating.gcc.js 

错误信息:

Firefox: 
$(".star1").rating is not a function 
callback: function (value) { 
jquery.ratingSampleCode.js (line 9) 

Chrome: 
Uncaught TypeError: Object [object Object] has no method 'rating' 
jquery.ratingSampleCode.js:8 

从我的示例代码:

$('.star1').rating({ 
    callback: function (value) { 

测试:http://prefabsoftware.com/test/rating-july15/

下载:prefabsoftware.com/test/rating-july15.zip

一些有用的链接:(这我不能指定为降价,因为我不能”牛逼登录与我的老口碑点...)

  • 高级编辑和实习医生:developers.google.com/closure/compiler/docs/api-tutorial3#externs
  • 样品实习医生:contrib请:代码。 google.com/p/closure-compiler/source/browse/#svn%2Ftrunk%2Fcontrib%2Fexterns)包括jQuery本身,但不包括评级插件
  • 更多externs:code.google.com/p/closure-compiler/源/浏览/#svn%2Ftrunk%2Fexterns

是否有一个简单的修复extern?还是更好的解决方案?

谢谢!


好吧,这个作品为实习医生文件:

$.prototype.rating = function(arg1) {} 
jQuery.prototype.rating = function(arg1) {} 

$.prototype.rating.focus = function() {} 
... etc. 
+0

我看到:我应该实际编译之外的所有插件,只是引用通常的方式。并且,非常感谢外部提示;我添加了以上工作代码,因为我无法添加此评论。 – 2012-07-18 10:49:07

回答

1

从你的描述,你似乎是使用一个外部文件不正确。您的插件的外部文件将允许其他用户编译引用您的插件的代码。它不应该被用来编译你实际的插件代码。要编译你的代码,你只需要jQuery extern文件。

jQuery代码样式与Closure编译器有已知的问题。特别是,您需要避免以下情况:

  • 任何使用$别名。使用完整的jQuery命名空间。编译器不能很好地处理别名命名空间。
  • jQuery.fn别名。请使用jQuery.prototype
  • 使用jQuery.extend方法添加函数原型或公共方法。相反,将它们直接添加到原型。 (例如:jQuery.fn.extend({a: 'foo'});将变成jQuery.prototype.a = 'foo';);

随着ADVANCED_OPTIMIZATIONS,请记住,您仍然必须导出或引用任何公共方法和原型。这可能意味着SIMPLE_OPTIMIZATIONS变得更适合您的项目。

欲了解更多信息,请参阅http://blogs.missouristate.edu/web/2011/02/14/jquery-plugins-and-closure-compiler/

相关问题