Skip to content

Commit

Permalink
Widget: Added ._bind() for easily binding events with correct context…
Browse files Browse the repository at this point in the history
… and disabled checking. Pretty much a direct copy from the previous bind branch.
  • Loading branch information
scottgonzalez committed Jan 18, 2011
1 parent 67b070f commit 659db70
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/unit/widget/widget_core.js
Expand Up @@ -404,6 +404,99 @@ test( ".widget() - overriden", function() {
same( wrapper[0], $( "<div></div>" ).testWidget().testWidget( "widget" )[0] );
});

test( "_bind to element (default)", function() {
expect( 12 );
var self;
$.widget( "ui.testWidget", {
_create: function() {
self = this;
this._bind({
keyup: this.keyup,
keydown: this.keydown
});
},
keyup: function( event ) {
equals( self, this );
equals( self.element[0], event.currentTarget );
equals( "keyup", event.type );
},
keydown: function( event ) {
equals( self, this );
equals( self.element[0], event.currentTarget );
equals( "keydown", event.type );
}
});
var widget = $( "<div></div>" )
.testWidget()
.trigger( "keyup" )
.trigger( "keydown" );
widget
.testWidget( "disable" )
.trigger( "keyup" )
.trigger( "keydown" );
widget
.testWidget( "enable" )
.trigger( "keyup" )
.trigger( "keydown" );
widget
.testWidget( "destroy" )
.trigger( "keyup" )
.trigger( "keydown" );
});

test( "_bind to descendent", function() {
expect( 12 );
var self;
$.widget( "ui.testWidget", {
_create: function() {
self = this;
this._bind( this.element.find( "strong" ), {
keyup: this.keyup,
keydown: this.keydown
});
},
keyup: function( event ) {
equals( self, this );
equals( self.element.find( "strong" )[0], event.currentTarget );
equals( "keyup", event.type );
},
keydown: function(event) {
equals( self, this );
equals( self.element.find( "strong" )[0], event.currentTarget );
equals( "keydown", event.type );
}
});
// trigger events on both widget and descendent to ensure that only descendent receives them
var widget = $( "<div><p><strong>hello</strong> world</p></div>" )
.testWidget()
.trigger( "keyup" )
.trigger( "keydown" );
var descendent = widget.find( "strong" )
.trigger( "keyup" )
.trigger( "keydown" );
widget
.testWidget( "disable" )
.trigger( "keyup" )
.trigger( "keydown" );
descendent
.trigger( "keyup" )
.trigger( "keydown" );
widget
.testWidget( "enable" )
.trigger( "keyup" )
.trigger( "keydown" );
descendent
.trigger( "keyup" )
.trigger( "keydown" );
widget
.testWidget( "destroy" )
.trigger( "keyup" )
.trigger( "keydown" );
descendent
.trigger( "keyup" )
.trigger( "keydown" );
});

test( "._trigger() - no event, no ui", function() {
expect( 7 );
var handlers = [];
Expand Down
22 changes: 22 additions & 0 deletions ui/jquery.ui.widget.js
Expand Up @@ -129,6 +129,8 @@ $.Widget.prototype = {
this._getCreateOptions(),
options );

this.bindings = $();

var self = this;
this.element.bind( "remove." + this.widgetName, function() {
self.destroy();
Expand Down Expand Up @@ -162,6 +164,7 @@ $.Widget.prototype = {
.removeClass(
this.widgetBaseClass + "-disabled " +
"ui-state-disabled" );
this.bindings.unbind( "." + this.widgetName );
},
_destroy: $.noop,

Expand Down Expand Up @@ -216,6 +219,25 @@ $.Widget.prototype = {
return this._setOption( "disabled", true );
},

_bind: function( element, handlers ) {
// no element argument, shuffle and use this.element
if ( !handlers ) {
handlers = element;
element = this.element;
} else {
this.bindings = this.bindings.add( element );
}
var instance = this;
$.each( handlers, function( event, handler ) {
element.bind( event + "." + instance.widgetName, function() {
if ( instance.options.disabled ) {
return;
}
return handler.apply( instance, arguments );
});
});
},

_trigger: function( type, event, data ) {
var callback = this.options[ type ];

Expand Down

2 comments on commit 659db70

@jzaefferer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point we should remove those old bind branches...

Whats the deal with this new branch?

@scottgonzalez
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was having trouble getting bind2 to a reasonable state, so I started a new branch. I deleted the old bind branches as soon as I created this one. I wanted to play around with some other ideas. For example, this._bind({ click: "clickHandler" }) to bind to this.clickHandler but wait until the click to figure out what function we're executing. This allows us to bind to a function while still allowing the function to be proxied later, which is an important feature that "just works" when you have to manually store a reference to this and wrap your handler in an anonymous function.

I also want to test out implementing ._hoverable( elem ) and ._focusable( elem ) on top of ._bind() to remove a lot of the duplicated code across our widgets.

Please sign in to comment.