Skip to content

Commit 2a6ca3f

Browse files
committed
Widget: Add a _delay method. Will be used in various places to replace setTimeout with custom binding (mostly getting rid of var self/that)
1 parent d12180d commit 2a6ca3f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

tests/unit/widget/widget_core.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,4 +1040,28 @@ test( "redefine", function() {
10401040
equal( $.ui.testWidget.foo, "bar", "static properties remain" );
10411041
});
10421042

1043+
asyncTest( "_delay", function() {
1044+
expect( 4 );
1045+
var order = 0,
1046+
that;
1047+
$.widget( "ui.testWidget", {
1048+
defaultElement: null,
1049+
_create: function() {
1050+
that = this;
1051+
this._delay(function() {
1052+
strictEqual( this, that );
1053+
equal( order, 1 );
1054+
start();
1055+
}, 500);
1056+
this._delay("callback");
1057+
},
1058+
callback: function() {
1059+
strictEqual( this, that );
1060+
equal( order, 0 );
1061+
order += 1;
1062+
}
1063+
});
1064+
$( "#widget" ).testWidget();
1065+
});
1066+
10431067
}( jQuery ) );

ui/jquery.ui.widget.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,15 @@ $.Widget.prototype = {
333333
});
334334
},
335335

336+
_delay: function( handler, delay ) {
337+
function handlerProxy() {
338+
return ( typeof handler === "string" ? instance[ handler ] : handler )
339+
.apply( instance, arguments );
340+
}
341+
var instance = this;
342+
setTimeout( handlerProxy, delay || 0 );
343+
},
344+
336345
_hoverable: function( element ) {
337346
this.hoverable = this.hoverable.add( element );
338347
this._bind( element, {

0 commit comments

Comments
 (0)