Skip to content

Commit

Permalink
Widget: Add a _delay method. Will be used in various places to replac…
Browse files Browse the repository at this point in the history
…e setTimeout with custom binding (mostly getting rid of var self/that)
  • Loading branch information
jzaefferer committed Sep 12, 2011
1 parent d12180d commit 2a6ca3f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/unit/widget/widget_core.js
Expand Up @@ -1040,4 +1040,28 @@ test( "redefine", function() {
equal( $.ui.testWidget.foo, "bar", "static properties remain" );
});

asyncTest( "_delay", function() {
expect( 4 );
var order = 0,
that;
$.widget( "ui.testWidget", {
defaultElement: null,
_create: function() {
that = this;
this._delay(function() {
strictEqual( this, that );
equal( order, 1 );
start();
}, 500);
this._delay("callback");
},
callback: function() {
strictEqual( this, that );
equal( order, 0 );
order += 1;
}
});
$( "#widget" ).testWidget();
});

}( jQuery ) );
9 changes: 9 additions & 0 deletions ui/jquery.ui.widget.js
Expand Up @@ -333,6 +333,15 @@ $.Widget.prototype = {
});
},

_delay: function( handler, delay ) {
function handlerProxy() {
return ( typeof handler === "string" ? instance[ handler ] : handler )
.apply( instance, arguments );
}
var instance = this;
setTimeout( handlerProxy, delay || 0 );
},

_hoverable: function( element ) {
this.hoverable = this.hoverable.add( element );
this._bind( element, {
Expand Down

0 comments on commit 2a6ca3f

Please sign in to comment.