Browse files

Rename $.extend2 to $.widget.extend

  • Loading branch information...
1 parent 8b06707 commit 6411cdf9667e4165bdd206db6b31619dac4dbfa4 @jzaefferer jzaefferer committed May 2, 2011
Showing with 41 additions and 42 deletions.
  1. +1 −1 tests/unit/widget/widget.html
  2. +16 −16 tests/unit/widget/{extend2.js → widget_extend.js}
  3. +24 −25 ui/jquery.ui.widget.js
View
2 tests/unit/widget/widget.html
@@ -14,7 +14,7 @@
<script src="../testsuite.js"></script>
<script src="widget_core.js"></script>
- <script src="extend2.js"></script>
+ <script src="widget_extend.js"></script>
<script src="../swarminject.js"></script>
</head>
View
32 tests/unit/widget/extend2.js → tests/unit/widget/widget_extend.js
@@ -1,4 +1,4 @@
-test("jQuery.extend(Object, Object)", function() {
+test("$.widget.extend(Object, Object)", function() {
expect(28);
var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
@@ -13,62 +13,62 @@ test("jQuery.extend(Object, Object)", function() {
arr = [1, 2, 3],
nestedarray = { arr: arr };
- jQuery.extend2(settings, options);
+ $.widget.extend(settings, options);
same( settings, merged, "Check if extended: settings must be extended" );
same( options, optionsCopy, "Check if not modified: options must not be modified" );
- jQuery.extend2(settings, null, options);
+ $.widget.extend(settings, null, options);
same( settings, merged, "Check if extended: settings must be extended" );
same( options, optionsCopy, "Check if not modified: options must not be modified" );
- jQuery.extend2(deep1, deep2);
+ $.widget.extend(deep1, deep2);
same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
- ok( jQuery.extend2({}, nestedarray).arr === arr, "Don't clone arrays" );
- ok( jQuery.isPlainObject( jQuery.extend2({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" );
+ ok( $.widget.extend({}, nestedarray).arr === arr, "Don't clone arrays" );
+ ok( jQuery.isPlainObject( $.widget.extend({ arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" );
var empty = {};
var optionsWithLength = { foo: { length: -1 } };
- jQuery.extend2(empty, optionsWithLength);
+ $.widget.extend(empty, optionsWithLength);
same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
empty = {};
var optionsWithDate = { foo: { date: new Date } };
- jQuery.extend2(empty, optionsWithDate);
+ $.widget.extend(empty, optionsWithDate);
same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
var myKlass = function() {};
var customObject = new myKlass();
var optionsWithCustomObject = { foo: { date: customObject } };
empty = {};
- jQuery.extend2(empty, optionsWithCustomObject);
+ $.widget.extend(empty, optionsWithCustomObject);
ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
// Makes the class a little more realistic
myKlass.prototype = { someMethod: function(){} };
empty = {};
- jQuery.extend2(empty, optionsWithCustomObject);
+ $.widget.extend(empty, optionsWithCustomObject);
ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
- var ret = jQuery.extend2({ foo: 4 }, { foo: new Number(5) } );
+ var ret = $.widget.extend({ foo: 4 }, { foo: new Number(5) } );
ok( ret.foo == 5, "Wrapped numbers copy correctly" );
var nullUndef;
- nullUndef = jQuery.extend2({}, options, { xnumber2: null });
+ nullUndef = $.widget.extend({}, options, { xnumber2: null });
ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
- nullUndef = jQuery.extend2({}, options, { xnumber2: undefined });
+ nullUndef = $.widget.extend({}, options, { xnumber2: undefined });
ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
- nullUndef = jQuery.extend2({}, options, { xnumber0: null });
+ nullUndef = $.widget.extend({}, options, { xnumber0: null });
ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
// TODO weird test
/*
var target = {};
var recursive = { foo:target, bar:5 };
- jQuery.extend2(target, recursive);
+ $.widget.extend(target, recursive);
same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
*/
@@ -106,7 +106,7 @@ test("jQuery.extend(Object, Object)", function() {
var input = {
key: [ 1, 2, 3 ]
}
- var output = jQuery.extend2( {}, input );
+ var output = $.widget.extend( {}, input );
deepEqual( input, output, "don't clone arrays" );
input.key[0] = 10;
deepEqual( input, output, "don't clone arrays" );
View
49 ui/jquery.ui.widget.js
@@ -11,24 +11,6 @@
var slice = Array.prototype.slice;
-function extend( target ) {
- var input = slice.call( arguments, 1 ),
- inputIndex = 0,
- inputLength = input.length,
- key,
- value;
- for ( ; inputIndex < inputLength; inputIndex++ ) {
- for ( key in input[ inputIndex ] ) {
- value = input[ inputIndex ][ key ];
- if (input[ inputIndex ].hasOwnProperty( key ) && value !== undefined ) {
- target[ key ] = $.isPlainObject( value ) ? extend( {}, target[ key ], value ) : value;
- }
- }
- }
- return target;
-}
-$.extend2 = extend;
-
var _cleanData = $.cleanData;
$.cleanData = function( elems ) {
for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
@@ -56,7 +38,7 @@ $.widget = function( name, base, prototype ) {
$[ namespace ] = $[ namespace ] || {};
// create the constructor using $.extend() so we can carry over any
// static properties stored on the existing constructor (if there is one)
- $[ namespace ][ name ] = $.extend( function( options, element ) {
+ $[ namespace ][ name ] = $.widget.extend( function( options, element ) {
// allow instantiation without "new" keyword
if ( !this._createWidget ) {
return new $[ namespace ][ name ]( options, element );
@@ -73,7 +55,7 @@ $.widget = function( name, base, prototype ) {
// 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
- basePrototype.options = extend( {}, basePrototype.options );
+ basePrototype.options = $.widget.extend( {}, basePrototype.options );
$.each( prototype, function( prop, value ) {
if ( $.isFunction( value ) ) {
prototype[ prop ] = (function() {
@@ -101,7 +83,7 @@ $.widget = function( name, base, prototype ) {
}());
}
});
- $[ namespace ][ name ].prototype = extend( basePrototype, {
+ $[ namespace ][ name ].prototype = $.widget.extend( basePrototype, {
namespace: namespace,
widgetName: name,
widgetEventPrefix: name,
@@ -111,6 +93,23 @@ $.widget = function( name, base, prototype ) {
$.widget.bridge( name, $[ namespace ][ name ] );
};
+$.widget.extend = function( target ) {
+ var input = slice.call( arguments, 1 ),
+ inputIndex = 0,
+ inputLength = input.length,
+ key,
+ value;
+ for ( ; inputIndex < inputLength; inputIndex++ ) {
+ for ( key in input[ inputIndex ] ) {
+ value = input[ inputIndex ][ key ];
+ if (input[ inputIndex ].hasOwnProperty( key ) && value !== undefined ) {
+ target[ key ] = $.isPlainObject( value ) ? $.widget.extend( {}, target[ key ], value ) : value;
+ }
+ }
+ }
+ return target;
+};
+
$.widget.bridge = function( name, object ) {
$.fn[ name ] = function( options ) {
var isMethodCall = typeof options === "string",
@@ -119,7 +118,7 @@ $.widget.bridge = function( name, object ) {
// allow multiple hashes to be passed on init
options = !isMethodCall && args.length ?
- extend.apply( null, [ options ].concat(args) ) :
+ $.widget.extend.apply( null, [ options ].concat(args) ) :
options;
if ( isMethodCall ) {
@@ -181,7 +180,7 @@ $.Widget.prototype = {
_createWidget: function( options, element ) {
element = $( element || this.defaultElement || this )[ 0 ];
this.element = $( element );
- this.options = extend( {},
+ this.options = $.widget.extend( {},
this.options,
this._getCreateOptions(),
options );
@@ -236,7 +235,7 @@ $.Widget.prototype = {
if ( arguments.length === 0 ) {
// don't return a reference to the internal hash
- return extend( {}, this.options );
+ return $.widget.extend( {}, this.options );
}
if ( typeof key === "string" ) {
@@ -248,7 +247,7 @@ $.Widget.prototype = {
parts = key.split( "." );
key = parts.shift();
if ( parts.length ) {
- curOption = options[ key ] = extend( {}, this.options[ key ] );
+ curOption = options[ key ] = $.widget.extend( {}, this.options[ key ] );
for ( i = 0; i < parts.length - 1; i++ ) {
curOption[ parts[ i ] ] = curOption[ parts[ i ] ] || {};
curOption = curOption[ parts[ i ] ];

0 comments on commit 6411cdf

Please sign in to comment.