Widget: Add a _delay method. Will be used in various places to replace setTimeout with custom binding (mostly getting rid of var self/that)

This commit is contained in:
Jörn Zaefferer 2011-09-12 23:37:14 +02:00
parent d12180d1a5
commit 2a6ca3fb39
2 changed files with 33 additions and 0 deletions

View File

@ -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 ) );

View File

@ -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, {