Skip to content

Instantly share code, notes, and snippets.

@chrisabrams
Forked from ehynds/widget-template.js
Created January 14, 2012 18:43
Show Gist options
  • Select an option

  • Save chrisabrams/1612469 to your computer and use it in GitHub Desktop.

Select an option

Save chrisabrams/1612469 to your computer and use it in GitHub Desktop.

Revisions

  1. ehynds revised this gist Apr 22, 2010. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions widget-template.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    (function($){

    $.widget("ui.mywidget", {
    @@ -72,7 +71,7 @@
    $.ui.mywidget.instances.splice(position, 1);
    }

    // call the original destroy method since we overwrite it
    // call the original destroy method since we overwrote it
    $.Widget.prototype.destroy.call( this );
    },

  2. ehynds revised this gist Apr 22, 2010. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions widget-template.js
    Original file line number Diff line number Diff line change
    @@ -71,6 +71,9 @@
    if(position > -1){
    $.ui.mywidget.instances.splice(position, 1);
    }

    // call the original destroy method since we overwrite it
    $.Widget.prototype.destroy.call( this );
    },

    _getOtherInstances: function(){
  3. ehynds revised this gist Apr 22, 2010. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions widget-template.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    (function($){

    $.widget("ui.mywidget", {
    @@ -61,6 +62,17 @@
    return this._isOpen;
    },

    destroy: function(){
    // remove this instance from $.ui.mywidget.instances
    var element = this.element,
    position = $.inArray(element, $.ui.mywidget.instances);

    // if this instance was found, splice it off
    if(position > -1){
    $.ui.mywidget.instances.splice(position, 1);
    }
    },

    _getOtherInstances: function(){
    var element = this.element;

  4. ehynds created this gist Apr 21, 2010.
    88 changes: 88 additions & 0 deletions widget-template.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    (function($){

    $.widget("ui.mywidget", {
    options: {
    autoOpen: true
    },

    _create: function(){

    // by default, consider this thing closed.
    this._isOpen = false;

    // remember this instance
    $.ui.mywidget.instances.push(this.element);
    },

    _init: function(){

    // call open if this instance should be open by default
    if( this.options.autoOpen ){
    this.open();
    }
    },

    open: function(){
    this._isOpen = true;

    // trigger beforeopen event. if beforeopen returns false,
    // prevent bail out of this method.
    if( this._trigger("beforeopen") === false ){
    return;
    }

    // call methods on every other instance of this dialog
    $.each( this._getOtherInstances(), function(){
    var $this = $(this);

    if($this.mywidget("isOpen")){
    $this.mywidget("close");
    }
    });

    // more open related code here

    // trigger open event
    this._trigger("open");

    return this;
    },

    close: function(){
    this._isOpen = false;

    // trigger close event
    this._trigger("close");

    return this;
    },

    isOpen: function(){
    return this._isOpen;
    },

    _getOtherInstances: function(){
    var element = this.element;

    return $.grep($.ui.mywidget.instances, function(el){
    return el !== element;
    });
    },

    _setOption: function(key, value){
    this.options[key] = value;

    switch(key){
    case "something":
    // perform some additional logic if just setting the new
    // value in this.options is not enough.
    break;
    }
    }
    });

    $.extend($.ui.mywidget, {
    instances: []
    });

    })(jQuery);