Reno
Reno

Reputation: 2982

UI Dialog window - How to not display dialog until content is loaded?

I've been experimenting with using a UI Dialog window to display an existing form (page with the form and no layout). I'm not sure if I'm doing this the right way but it seems to work fine minus a couple of pieces of bad behavior. This is the behavior right now:

  1. The dialog window with no conent opens (very small/empty)
  2. About a half second later the content loads and the window expands insantly (looks bad visually)
  3. Even though the window width expands when the content is loaded, the title bar does not adjust to the new width and remains very small width wise. Although if resizeable is set to 'true' the title will expand. It's just not responding with a width adjustment when the content from the hidden div is loaded.

How could I go about not displaying the dialog until the content is finished loading into the dialog, and how could I force the dialog title to width adjust right after the content is finished loading?

<A HREF="javascript:newItem('foo')">CREATE NEW FOO ITEM<A>

<script type="text/javascript">
    newItem= function(type) {
      $("#form_load").load(
          '/items/new', {item_type: type}).dialog({
                modal:true,
                draggable: true,
                resizable: false,
                width:'auto',
                height:'auto',
                title: 'Some title',
                position: [150, 150]
          });
    };
</script>

Thanks!

Upvotes: 3

Views: 2710

Answers (1)

McHerbie
McHerbie

Reputation: 2995

You're going to want to call the dialog after the content is loading. You can do this by using $.load's callback method.

<script type="text/javascript">
    newItem = function(type) {
        $("#form_load").hide().load(
            '/items/new',
            {item_type: type},
            function (data) {
                $(this).dialog({
                    modal     : true,
                    draggable : true,
                    resizable : false,
                    width     : 'auto',
                    height    : 'auto',
                    title     : 'Some title',
                    position  : [100, 100]
                });
            }
        });            
    }
</script>

You may also want to include a hide/show toggle so the #form_load container isn't displayed until the content is loaded.

See jQuery's $.load docs here: http://api.jquery.com/load/

UPDATE: Added .hide() so content isn't shown until dialog is created.

Upvotes: 3

Related Questions