Skip to content

Instantly share code, notes, and snippets.

@ehynds
Created April 16, 2010 12:38
Show Gist options
  • Save ehynds/368363 to your computer and use it in GitHub Desktop.
Save ehynds/368363 to your computer and use it in GitHub Desktop.
// accessing all OTHER instances of a widget, from within your widget.
// method one: use your own data store
var instances = [];
$.widget("ui.dialog", {
_create: function(){
instances.push(this.element);
},
open: function(){
var self = this;
// call methods on every other instance of this dialog
$.each( self._getOtherInstances(), function(){
if( $(this).dialog("isOpen") ){
$(this).dialog("close");
}
});
},
_getOtherInstances: function(){
var self = this;
return $.grep(instances, function(el,i){
return el !== self.element;
});
}
});
// method two: store the original element inside the widget's data cache
$.widget("ui.dialog", {
_create: function(){
// uiDialog is the main dialog container (div.ui-dialog).
// store a reference to the original jQuery element object in it's data cache
this.uiDialog.data("originalelement", this.element);
},
open: function(){
// query the DOM for all ui-dialog's, excluding the current instance.
// retrieve the element the dialog is bound to, and fire the close method on it
this._getOtherInstances().each(function(){
var $elem = $(this).data("originalelement");
if( $elem.dialog("isOpen") ){
$elem.dialog("close");
}
});
},
_getOtherInstances: function(){
return $("div.ui-dialog").not(this.uiDialog);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment