views:

243

answers:

2

Hi,

I have a container and some actions according to

<div id="container"></div>

<a href="#" class="action" id="add">Add user</a>

<a href="#" class="action" id="view">View user</a>

Notice i use a unique container to load any page. When i click an action, it triggers a click event

// dialog settings
var settings = {
    add:{
        buttons:{
            "Add user":function() {
                // some action
            }
        },
        open:function(e, ui) {
            $(this).load("/add.xhtml");
        }
    },
    view:{
        buttons:{
            "View user":function() {
                // some action
            }
        },
        open:function(e, ui) {
            $(this).load("/view.xhtml");
        }
    }
};


$(".action").click(
    function(e) {
        e.preventDefault();

        $("#container").dialog(settings[$(this).attr("id")].dialog());
    }
);
+2  A: 

I think you'll have to approach things a bit differently. The dialog is initialized in your your add/view options. So, the javascript is parsed and initialized the first dialog and ignores the second because an instance already exists.

Initialize the dialog somewhere else, and load the html into the container instead, then trigger the dialog open.

var dialog = {    
    add: $("#container").load("/add.xhtml"),    
    view: $("#container").load("/view.xhtml")
};

and .dialog("open") the container after, or perhaps restructure your markup to make this more readable, such as:

$("#container").load("/" + $(this).attr("id") + ".xhtml").dialog("open");
ScottE
Thanks, Scott. And when my dialog has buttons ? What should i do ? If i put .load("/" + $(this).attr("id") + ".xhtml").dialog("open") i need to know which dialog has been triggered
Arthur Ronald F D Garcia
You can add the buttons by using the setter instead of the instantiation. Look to http://jqueryui.com/demos/dialog/#method-option
ScottE
('.selector').dialog('option', 'buttons', { "Ok": function() { $(this).dialog("close"); } });
ScottE
A: 

If i insert close event in each dialog according to

close:function(e, ui) {
    $(this).dialog("destroy");
}

It works fine!

Arthur Ronald F D Garcia