淘先锋技术网

首页 1 2 3 4 5 6 7

jQuery.ui 对象属性
$.ui Object
  1. accordionfunction (h,g){arguments.length&&this._createWidget(h,g)}
  2. autocompletefunction (h,g){arguments.length&&this._createWidget(h,g)}
  3. buttonfunction (h,g){arguments.length&&this._createWidget(h,g)}
  4. buttonsetfunction (h,g){arguments.length&&this._createWidget(h,g)}
  5. containsfunction (a,b){return document.compareDocumentPosition?a.compareDocumentPosition(b)&16:a!==b&&a.contains(b)}
  6. datepickerObject
  7. ddmanagerObject
  8. dialogfunction (h,g){arguments.length&&this._createWidget(h,g)}
  9. draggablefunction (h,g){arguments.length&&this._createWidget(h,g)}
  10. droppablefunction (h,g){arguments.length&&this._createWidget(h,g)}
  11. hasScrollfunction (a,b){if(c(a).css("overflow")==="hidden")return false;b=b&&b==="left"?"scrollLeft":"scrollTop";var d=false;if(a[b]>0)return true;a[b]=1;d=a[b]>0;a[b]=0;return d}
  12. intersectfunction (a,b,c){if(!b.offset)return false;var e=(a.positionAbs||a.position.absolute).left,g=e+a.helperProportions.width,f=(a.positionAbs||a.position.absolute).top,h=f+a.helperProportions.height,i=b.offset.left,k=i+b.proportions.width,j=b.offset.top,l=j+b.proportions.height;
  13. isOverfunction (a,b,d,e,h,i){return c.ui.isOverAxis(a,d,h)&&c.ui.isOverAxis(b,e,i)}
  14. isOverAxisfunction (a,b,d){return a>b&&a<b+d}
  15. keyCodeObject
  16. menufunction (h,g){arguments.length&&this._createWidget(h,g)}
  17. mousefunction (h,g){arguments.length&&this._createWidget(h,g)}
  18. pluginObject
  19. positionObject
  20. progressbarfunction (h,g){arguments.length&&this._createWidget(h,g)}
  21. resizablefunction (h,g){arguments.length&&this._createWidget(h,g)}
  22. selectablefunction (h,g){arguments.length&&this._createWidget(h,g)}
  23. sliderfunction (h,g){arguments.length&&this._createWidget(h,g)}
  24. sortablefunction (h,g){arguments.length&&this._createWidget(h,g)}
  25. tabsfunction (h,g){arguments.length&&this._createWidget(h,g)}
  26. version"1.8.13"
  27. __proto__Object
  

jQuery UI的设计也使用了面向对象的思想,javaScript采用prototype继承,其语法与C++/Java有很大不同,源码不能直观的显示框架的类图关系:


  • super class为$.Widget.prototype,它实现了jQuery UI的基本功能。
  • Widget之间也存在继承关系,比如$.ui.draggable就继承了$.ui.mouse函数


重要的函数
$.widget
  • $.widget = function(name, base, prototype)
  • 这是widget运行的框架,是面向对象的框架
    • $.Widget.prototype是所有$.namespace.name 的父对象
    • 输入参数base为$.namespace.name的子对象
    • prototype是对base的扩展和函数覆盖(extend可以用来实现多态啊)
  • 以draggable举例说明
    • $(selector).draggable(options) 调用过后,这个$(selector)对象将拥有一个独有的带options配置对象的$.ui.draggable对象
      • 在 $.fn.draggable构造函数中,将 new $.ui.draggable(options) 对象给绑定给了this的draggable名字
        • 调用了 $.data(this, name,new object(options, this ) );
      • 在$.fn.draggable构造函数中,为$(selector) dom对象绑定了drag的事件event handle

//输入参数 base 为一种 $.widget对象
//prototype为其它对象
$.widget = function( name, base, prototype ) {
	var namespace = name.split( "." )[ 0 ],
		fullName;
	name = name.split( "." )[ 1 ];
	fullName = namespace + "-" + name;

	if ( !prototype ) {
		prototype = base;
		base = $.Widget;
	}

	// create selector for plugin
	$.expr[ ":" ][ fullName ] = function( elem ) {
		return !!$.data( elem, name );
	};

	$[ namespace ] = $[ namespace ] || {};
	$[ namespace ][ name ] = function( options, element ) {
		// allow instantiation without initializing for simple inheritance
		if ( arguments.length ) {
			this._createWidget( options, element );
		}
	};

	var basePrototype = new base();

        //这是 $.ui.name 比如 $.ui.draggable的初始化代码,它将被绑定到每个DOM元素去
        $[ namespace ][ name ] = function( options, element ) {
		// allow instantiation without initializing for simple inheritance
		if ( arguments.length ) {
			this._createWidget( options, element );  //这是个关键的函数~
                                                                 //options 会被 extend 到 $.namespace.name对象中去
		}
	};

	var basePrototype = new base();
	// we need to make the options hash a property directly on the new instance
	// otherwise we'll modify the options hash on the prototype that we're
	// inheriting from
//	$.each( basePrototype, function( key, val ) {
//		if ( $.isPlainObject(val) ) {
//			basePrototype[ key ] = $.extend( {}, val );
//		}
//	});
	basePrototype.options = $.extend( true, {}, basePrototype.options );
	$[ namespace ][ name ].prototype = $.extend( true, basePrototype, {
		namespace: namespace,
		widgetName: name,
		widgetEventPrefix: $[ namespace ][ name ].prototype.widgetEventPrefix || name,
		widgetBaseClass: fullName
	}, prototype );

	$.widget.bridge( name, $[ namespace ][ name ] ); 

$.widget.bridge = function( name, object ) {      //object是 $.ui.name 静态函数,这个object将被绑定到每个调用$(selector).name()的jqeury DOM对象中
	$.fn[ name ] = function( options ) {                                    //这里定义了ui plugin,比如 $.fn.draggable,是使用UI的入口
		var isMethodCall = typeof options === "string",                 //如果参数为字符串,确定是函数调用
			args = Array.prototype.slice.call( arguments, 1 ),
			returnValue = this;

		// allow multiple hashes to be passed on init
		options = !isMethodCall && args.length ?
			$.extend.apply( null, [ true, options ].concat(args) ) :
			options;

		// prevent calls to internal methods
		if ( isMethodCall && options.charAt( 0 ) === "_" ) {  //确保不调用内部函数,JS语言没有C++的private访问控制啊~
			return returnValue;
		}

		if ( isMethodCall ) {
			this.each(function() {
				var instance = $.data( this, name ),             //取出依附在DOM中的object
					methodValue = instance && $.isFunction( instance[options] ) ?
						instance[ options ].apply( instance, args ) :
						instance;
				// TODO: add this back in 1.9 and use $.error() (see #5972)
				if ( methodValue !== instance && methodValue !== undefined ) {
					returnValue = methodValue;
					return false;
				}
			});
		} else {
			this.each(function() {
				var instance = $.data( this, name );
				if ( instance ) {
					instance.option( options || {} )._init();
				} else {
					$.data( this, name, new object( options, this ) );  
                                        //依附一个对象给这个DOM元素,供以后使用
                                        //这个对象就是 $.namespace.name(option, this)调用 _createWidget生成的,其prototype为
				}
			});
		}

		return returnValue;
	};
};