Javascript 如何在高级编译模式下使用Google Closure编译器编译jQuery UI小部件?

Javascript 如何在高级编译模式下使用Google Closure编译器编译jQuery UI小部件?,javascript,jquery,jquery-ui,google-closure-compiler,Javascript,Jquery,Jquery Ui,Google Closure Compiler,给出此示例jQuery UI小部件,可在以下位置找到: 编译器返回19个警告: Number of warnings: 19 JSC_INEXISTENT_PROPERTY: Property widget never defined on $ at line 4 character 0 $.widget( "custom.colorize", { ^ JSC_INEXISTENT_PROPERTY: Property addClass never defined on this.elemen

给出此示例jQuery UI小部件,可在以下位置找到:

编译器返回19个警告:

Number of warnings: 19

JSC_INEXISTENT_PROPERTY: Property widget never defined on $ at line 4 character 0
$.widget( "custom.colorize", {
^
JSC_INEXISTENT_PROPERTY: Property addClass never defined on this.element at line 18 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property disableSelection never defined on ? at line 18 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property appendTo never defined on ? at line 24 character 15
this.changer = $( "<button>", {
               ^
JSC_INEXISTENT_PROPERTY: Property css never defined on this.element at line 46 character 0
this.element.css( "background-color", "rgb(" +
^
JSC_INEXISTENT_PROPERTY: Property _trigger never defined on this at line 53 character 0
this._trigger( "change" );
^
JSC_INEXISTENT_PROPERTY: Property _trigger never defined on this at line 66 character 5
if ( this._trigger( "random", event, colors ) !== false ) {
     ^
JSC_INEXISTENT_PROPERTY: Property option never defined on this at line 67 character 0
this.option( colors );
^
JSC_INEXISTENT_PROPERTY: Property css never defined on ? at line 77 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property enableSelection never defined on ? at line 77 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property removeClass never defined on this.element at line 77 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property Widget never defined on $ at line 87 character 0
$.Widget.prototype._setOptions.apply( this, arguments );
^
JSC_INEXISTENT_PROPERTY: Property Widget never defined on $ at line 98 character 0
$.Widget.prototype._setOption.call( this, key, value );
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 103 character 0
$( "#my-widget1" ).colorize();
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 106 character 0
$( "#my-widget2" ).colorize({
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 113 character 0
$( "#my-widget3" ).colorize( {
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 123 character 0
$( ":custom-colorize" ).colorize( "disable" );
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 125 character 0
$( ":custom-colorize" ).colorize( "enable" );
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 130 character 0
$( ":custom-colorize" ).colorize( "option", {
^
警告数量:19
JSC_不存在_属性:在第4行字符0处的$上从未定义属性小部件
$.widget(“自定义.colorize”{
^
JSC_不存在_属性:在第18行字符0处的this.element上从未定义属性addClass
这个元素
^
JSC_不存在_属性:属性disableSelection从未在第18行定义字符0
这个元素
^
JSC_不存在_属性:属性appendTo从未在?上定义,第24行字符15
this.changer=$(“”{
^
JSC_不存在_属性:在第46行字符0处的this.element上从未定义属性css
css(“背景色”、“rgb(”+
^
JSC_不存在_属性:属性_触发器从未在第53行定义字符0
本._触发(“变更”);
^
JSC_不存在_属性:属性_触发器从未在第66行第5个字符处定义
如果(此._触发器(“随机”、事件、颜色)!==false){
^
JSC_不存在_属性:在第67行字符0处从未定义属性选项
此选项(颜色);
^
JSC_不存在_属性:属性css从未在第77行定义字符0
这个元素
^
JSC_不存在_属性:属性enableSelection从未在?第77行定义字符0
这个元素
^
JSC_不存在_属性:在第77行字符0处从未在此.element上定义属性removeClass
这个元素
^
JSC_不存在_属性:属性小部件从未在$87行定义字符0
$.Widget.prototype.\u setOptions.apply(这是参数);
^
JSC_不存在_属性:属性小部件从未在$98行定义字符0
$.Widget.prototype.\u setOption.call(this,key,value);
^
JSC_不存在_属性:在第103行字符0处的?上从未定义属性着色
$(“#my-widget1”).colorize();
^
JSC_不存在_属性:在第106行字符0处的?上从未定义属性着色
$(“#my-widget2”)。着色({
^
JSC_不存在_属性:在第113行字符0处的?上从未定义属性着色
$(“#my-widget3”)。着色({
^
JSC_不存在_属性:在第123行字符0处的?上从未定义属性着色
$(“:自定义着色”)。着色(“禁用”);
^
JSC_不存在_属性:在第125行字符0处的?上从未定义属性着色
$(“:自定义着色”)。着色(“启用”);
^
JSC_不存在_属性:在第130行字符0处的?上从未定义属性着色
$(“:自定义着色”)。着色(“选项”{
^
如何在高级编译模式下使用Google Closure Compiler编译jQuery UI小部件(基于)?如何解决编译器引发的警告?

除了,您还需要一个jQuery UI外部文件。这是定义jQuery UI向编译器提供的符号和类型信息所必需的

遗憾的是,目前没有已知的jQuery UI外部文件。您可以选择创建足够的外部文件供您使用或使用简单的优化。

除了,您还需要一个jQuery UI外部文件。这是定义jQuery UI向编译器提供的符号和类型信息所必需的


不幸的是,目前没有已知的jQuery UI外部文件。您可以选择创建足够的外部文件供您使用或使用简单的优化。

我想最好使用闭包库重写我的jQuery UI插件,并停止使用jQuery和jQuery UI。每个库都有其优点和缺点。选择装备ht库的工作和什么对你有意义。我个人不会改变库,因为它不适合与压缩器一起工作。对于基于jQuery的代码,我建议使用简单的闭包编译器优化。考虑到jQuery UI的流行和它的明显好处,我很惊讶这种需求这么长时间都没有得到满足他使用了闭包工具集。我想最好使用闭包库重写我的jQuery UI插件,停止使用jQuery和jQuery UI。每个库都有它的优点和缺点。为这项工作选择合适的库,并选择对您有意义的库。我个人不会更改库,因为库不能与压缩器一起使用。Fo在基于jQuery的代码中,我建议对闭包编译器使用简单的_优化。考虑到jQuery UI的流行和闭包工具集的明显优势,我很惊讶这种需求在这么长时间内一直没有得到满足。
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @extern https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @extern https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js
// @formatting pretty_print
// ==/ClosureCompiler==

$(function() {
// the widget definition, where "custom" is the namespace,
// "colorize" the widget name
$.widget( "custom.colorize", {
// default options
options: {
red: 255,
green: 0,
blue: 0,

// callbacks
change: null,
random: null
},

// the constructor
_create: function() {
this.element
// add a class for theming
.addClass( "custom-colorize" )
// prevent double click to select text
.disableSelection();

this.changer = $( "<button>", {
text: "change",
"class": "custom-colorize-changer"
})
.appendTo( this.element )
.button();

// bind click events on the changer button to the random method
// in 1.9 would use this._bind( this.changer, { click: "random" });
var that = this;
this.changer.bind("click.colorize", function() {
// _bind would handle this check
if (that.options.disabled) {
return;
}
that.random.apply(that, arguments);
});
this._refresh();
},

// called when created, and later when changing options
_refresh: function() {
this.element.css( "background-color", "rgb(" +
this.options.red +"," +
this.options.green + "," +
this.options.blue + ")"
);

// trigger a callback/event
this._trigger( "change" );
},

// a public method to change the color to a random value
// can be called directly via .colorize( "random" )
random: function( event ) {
var colors = {
red: Math.floor( Math.random() * 256 ),
green: Math.floor( Math.random() * 256 ),
blue: Math.floor( Math.random() * 256 )
};

// trigger an event, check if it's canceled
if ( this._trigger( "random", event, colors ) !== false ) {
this.option( colors );
}
},

// events bound via _bind are removed automatically
// revert other modifications here
_destroy: function() {
// remove generated elements
this.changer.remove();

this.element
.removeClass( "custom-colorize" )
.enableSelection()
.css( "background-color", "transparent" );
},

// _setOptions is called with a hash of all options that are changing
// always refresh when changing options
_setOptions: function() {
// in 1.9 would use _superApply
$.Widget.prototype._setOptions.apply( this, arguments );
this._refresh();
},

// _setOption is called for each individual option that is changing
_setOption: function( key, value ) {
// prevent invalid color values
if ( /red|green|blue/.test(key) && (value < 0 || value > 255) ) {
return;
}
// in 1.9 would use _super
$.Widget.prototype._setOption.call( this, key, value );
}
});

// initialize with default options
$( "#my-widget1" ).colorize();

// initialize with two customized options
$( "#my-widget2" ).colorize({
red: 60,
blue: 60
});

// initialize with custom green value
// and a random callback to allow only colors with enough green
$( "#my-widget3" ).colorize( {
green: 128,
random: function( event, ui ) {
return ui.green > 128;
}
});

// click to toggle enabled/disabled
$( "#disable" ).toggle(function() {
// use the custom selector created for each widget to find all instances
$( ":custom-colorize" ).colorize( "disable" );
}, function() {
$( ":custom-colorize" ).colorize( "enable" );
});

// click to set options after initalization
$( "#black" ).click( function() {
$( ":custom-colorize" ).colorize( "option", {
red: 0,
green: 0,
blue: 0
});
});
});
Number of warnings: 19

JSC_INEXISTENT_PROPERTY: Property widget never defined on $ at line 4 character 0
$.widget( "custom.colorize", {
^
JSC_INEXISTENT_PROPERTY: Property addClass never defined on this.element at line 18 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property disableSelection never defined on ? at line 18 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property appendTo never defined on ? at line 24 character 15
this.changer = $( "<button>", {
               ^
JSC_INEXISTENT_PROPERTY: Property css never defined on this.element at line 46 character 0
this.element.css( "background-color", "rgb(" +
^
JSC_INEXISTENT_PROPERTY: Property _trigger never defined on this at line 53 character 0
this._trigger( "change" );
^
JSC_INEXISTENT_PROPERTY: Property _trigger never defined on this at line 66 character 5
if ( this._trigger( "random", event, colors ) !== false ) {
     ^
JSC_INEXISTENT_PROPERTY: Property option never defined on this at line 67 character 0
this.option( colors );
^
JSC_INEXISTENT_PROPERTY: Property css never defined on ? at line 77 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property enableSelection never defined on ? at line 77 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property removeClass never defined on this.element at line 77 character 0
this.element
^
JSC_INEXISTENT_PROPERTY: Property Widget never defined on $ at line 87 character 0
$.Widget.prototype._setOptions.apply( this, arguments );
^
JSC_INEXISTENT_PROPERTY: Property Widget never defined on $ at line 98 character 0
$.Widget.prototype._setOption.call( this, key, value );
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 103 character 0
$( "#my-widget1" ).colorize();
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 106 character 0
$( "#my-widget2" ).colorize({
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 113 character 0
$( "#my-widget3" ).colorize( {
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 123 character 0
$( ":custom-colorize" ).colorize( "disable" );
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 125 character 0
$( ":custom-colorize" ).colorize( "enable" );
^
JSC_INEXISTENT_PROPERTY: Property colorize never defined on ? at line 130 character 0
$( ":custom-colorize" ).colorize( "option", {
^