4

How can I do something like:

$("#some_div").dialog("doSomething");

And what that method should do is to add an extra icon in the titlebar

EDIT 1: I've tried this solution: the method gets called but I can't access the dialog object (maybe I'm doing something wrong)

Community
  • 1
  • 1
Matías Cánepa
  • 5,770
  • 4
  • 57
  • 97

2 Answers2

2

First, if you're adding an icon to the title bar I would suggest applying a class to that dialog box and styling it with CSS. Example:

$( "#some_div" ).dialog({ dialogClass: "someClass" });

If you'd still like to add a custom method, here's what the documentation says:

Supply a callback function to handle the create event as an init option.

$( ".selector" ).dialog({
   create: function(event, ui) { ... }
});

Bind to the create event by type: dialogcreate.

$( ".selector" ).bind( "dialogcreate", function(event, ui) { 
    ... 
});
technophobia
  • 2,644
  • 1
  • 20
  • 29
2

Ok, this is what I did:

//in a separate js file
$.ui.dialog.prototype.showExtraButton = function()
{
    this.uiDialogTitlebar.append("<a role='button' class='ui-dialog-titlebar-icon ui-dialog-titlebar-icon-extra'><span class='ui-icon'></span></a>");
}

//call it this way
$("#some_div").dialog("showExtraButton");

//css
.ui-dialog-titlebar-icon {
  position: absolute;
  right: 25px;
}

.ui-dialog-titlebar-icon-extra span
{
    display: block;
    background-image: url(../path_to_img/button_extra.png)!important;
}

This solution by Langdon, along with this one by Kevin B gave me the answer on how to resolve my problem

UPDATE 2014-01-03

TIL about $.widget(), so here is another implementation of the same thing

$.widget("ui.dialog", $.ui.dialog,
{
    showExtraButton: function()
    {
        this.uiDialogTitlebar.append("<a role='button' class='ui-dialog-titlebar-icon ui-dialog-titlebar-icon-extra'><span class='ui-icon'></span></a>");
    }
});
Community
  • 1
  • 1
Matías Cánepa
  • 5,770
  • 4
  • 57
  • 97
  • Side Note: Try separating form from function. Unless that anchor (that's wrapping the `` serves a specific purpose, you're probably better off without it (i.e.: you can use a different tag instead). – technophobia Oct 02 '12 at 15:41