2012-07-15 63 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/