tags:

views:

306

answers:

1

I am trying to get dijits to render using the dojo.NodeList.instantiate method, which takes existing HTML elements and turns them into dijits when the DOM has loaded.

The API reference for the instantiate method can be found here.

The following example, which calls the instantiate method in the dojo.addOnLoad method, should create a BorderContainer with two ContentPane instances, but the DIVs remain as they start out, and do not become dijits:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <title>Dijit Test</title>
    <style type="text/css">
        @import "dojoroot/dijit/themes/tundra/tundra.css";
        @import "dojoroot/dojo/resources/dojo.css";
    </style>
    <script type="text/javascript" src="dojoroot/dojo/dojo.js"
        djConfig="parseOnLoad: true"></script>
    <script type="text/javascript">
        dojo.require("dijit.layout.BorderContainer");
        dojo.require("dijit.layout.ContentPane");

        dojo.addOnLoad(
            function() {
                dojo.query("#divOuter").instantiate(
                    dijit.layout.BorderContainer, {
                        design  : 'sidebar',
                        gutters : false
                    }
                );

                dojo.query("#divMiddle").instantiate(
                    dijit.layout.ContentPane, {
                        region  : 'center'
                    }
                );

                dojo.query("#divRight").instantiate(
                    dijit.layout.ContentPane, {
                        region   : 'right',
                        splitter    : true
                    }
                );
            }
        );
    </script>
</head>
<body>
    <div id="divOuter" style="width:400px;height:300px">
        <div id="divMiddle">Middle box</div>
        <div id="divRight">Right box</div>
    </div>
</body>
</html>

I have tried the above code in both Firefox 3.5 and Internet Explorer 7 and both fail to render the dijits. If I specify a standard HTML attribute in a property object (such as the style attribute), this style change appears correctly, indicating that the object is being read:

// The red border appears when using this example
dojo.query("#divRight").instantiate(
    dijit.layout.ContentPane, {
        region      :   'right',
        splitter    :   true,
        style       :   'border:1px solid red'
    }
);

The following HTML (using dojoType and other property attributes) works fine - the BorderContainer and ContentPanes appear correctly in both browsers:

<div dojoType="dijit.layout.BorderContainer" design="sidebar" gutters="false"
        style="width:400px;height:300px">
    <div dojoType="dijit.layout.ContentPane" region="center">Middle box</div>
    <div dojoType="dijit.layout.ContentPane" region="right" splitter="true" 
        style="width:200px;">Right box</div>
</div>

Please can anyone tell me why the instantiate example does not work?

I have done a lot of searching, but cannot seem to find anyone else with this issue, which may mean that I'm not using the instantiate method correctly!

Thanks.

+2  A: 

You need to call startup on those dijits after they are instantiated. dojo.query.instantiate doesn't do it for you since it just creates the objects.

At the bottom of your onLoad function, add these:

 dijit.byId('divOuter').startup();
 dijit.byId('divMiddle').startup();
 dijit.byId('divRight').startup();
seth
Thanks very much Seth - all working now!
You are welcome.
seth