tags:

views:

199

answers:

1

Instead of the typical custom widget that mixes in _Templated for it's markup, I want a widget that just mixes in _Widget then does an xhrGet to fetch the markup. In addition, and this is the part that I need help with, I want dojo to parse the xhr result text for dojoAttachPoint and dojoAttachEvent in the context of the widget, hookup up nodes and events.

The core of what I've tried for the widget is below:

dojo.provide("com.example.widget.DynamicAttachedTemplate");

dojo.require("dijit._Widget");
dojo.require("dojo.html");

dojo.declare("com.example.widget.DynamicAttachedTemplate", [dijit._Widget], {
  postCreate: function() {
    dojo.xhrGet({
      url: "/product/ajax/editTemplate",
      content: { id: 1 },
      handleAs: "text",
      preventCache: true,
      load: dojo.hitch(this, function(markup) {
        // this only parses markup for dojoType it seems :(
        dojo.html.set(this.srcNodeRef, markup, { parseContent: true });
      },
      error: function(error) {
        alert(error);
      }
    });
  },

  submitHandler: function(event) {
    dojo.stopEvent(event);
    // handle validation, form submit, etc.
  }
});                                                                      

And the page that uses it could look something like this:

...
<div id="productEditContainer">
  loading...
</div>
...
<script type="text/javascript">
  dojo.addOnLoad(function() {
    dojo.require("com.example.widget.DynamicAttachedTemplate");

    new com.example.widget.DynamicAttachedTemplate({}, dojo.byId("productEditContainer"));
  });
</script>

And let's say the markup returned from /product/ajax/editTemplate?id=1 was something like:

<form dojoAttachEvent="onsubmit: submitHandler">
  ...
</form>

In the end I want the dojoAttachEvent="onsubmit: submitHandler" in the xhr returned template markup to result in an event connect from the form submit to the widget submit handler method. I'm open to better approaches as well but the widget markup needs to be generated on the server and I'd really like to leverage dojoAttach* instead of using DOM IDs and manually hooking things up via dojo.byId in the widget setup code.

A: 

Hi, consider using inline template: http://www.sitepen.com/blog/2008/06/24/creating-dojo-widgets-with-inline-templates/.
However it requires _Templated.

Kniganapolke