eagerToLearn
eagerToLearn

Reputation: 429

Difference between dojoAttachpoint and id

<div dojoType="dojo.Dialog" id="alarmCatDialog" bgColor="#FFFFFF" bgOpacity="0.4" toggle="standard">
<div class='dijitInline'>
       <input type='input' class='dateWidgetInput' dojoAttachPoint='numberOfDateNode' selected="true">
</div>

how to show this dialog I tried dijit.byId('alarmCatDialog').show();

The above code is a template and I called dijit.byId('alarmCatDialog').show() from the .js file .

dojo.attr(this.numberOfDateNode) this code works and I got the data .but if I change dojoattachpoint to id then I try dijit.byId('numberOfDateNode') will not work;

Upvotes: 4

Views: 18696

Answers (2)

mschr
mschr

Reputation: 8641

It is important to keep track of what is the contents and which is the template of the dijit.Dialog. Once you set contents of a dialog, its markup is parsed - but not in a manner, such that the TemplatedMixin is applied to the content-markup-declared-widgets.

To successfully implement a template, you would need something similar to the following code, note that I've commented where attachPoints kicks in.

This SitePen blog renders nice info on the subject

define(
 [
   "dojo/declare",
    "dojo/_base/lang",
    "dijit/_Templated",
   "dijit/_Widget",
   "dijit/Dialog"
 ], function(
   declare,
    lang,
    _Templated,
    _Widget,
   Dialog
 ) {
  return declare("my.Dialog", [Dialog, _Templated], {
      // set any widget (Dialog construct) default parameters here
       toggle: 'standard',
        // render the dijit over a specific template
        // you should be aware, that once this templateString is overloaded,
        // then the one within Dialog is not rendered
      templateString: '<div bgColor="#FFFFFF" bgOpacity="0.4">' +// our domNode reference
                '<div class="dijitInline">' +
                    // setting a dojoAttachPoint makes it referencable from within widget by this attribute's value
                '  <input type="input" class="dateWidgetInput" dojoAttachPoint="numberOfDateNode" selected="true">' +
                '</div>' +
                '</div>',

        constructor: function(args, srcNodeRef) {
            args = args || {} // assert, we must mixin minimum an empty object
            lang.mixin(this, args);
        },
        postCreate: function() {

                    // with most overrides, preferred way is to call super functionality first
                    this.inherited(arguments);

            // here we can manipulate the contents of our widget, 
            // template parser _has run from this point forward
            var input = this.numberOfDateNode;
            // say we want to perform something on the numberOfDateNode, do so

        },
        // say we want to use dojo.Stateful pattern such that a call like
        //   myDialogInstance.set("dateValue", 1234)
        // will automatically set the input.value, do as follows
        _setDateValueAttr: function(val) {
            // NB: USING dojoAttachPoint REFERENCE
            this.numberOfDateNode.value = val;
        },
        // and in turn we must set up the getter
        _getDateValueAttr: function() {
            // NB: USING dojoAttachPoint REFERENCE
            return this.numberOfDateNode.value;
        }

  });

});

Upvotes: 0

phusick
phusick

Reputation: 7352

Your numberOfDateNode is a plain DOM node, not a widget/dijit, i.e. javascript object extending dijit/_Widget, which is the reason you cannot get a reference to it via dijit.byId("numberOfDateNode"). Use dojo.byId("numberOfDateNode") instead and you are all set.

dojoAttachPoint or its HTML5 valid version data-dojo-attach-point is being used inside a dijit template to attach a reference to DOM node or child dijit to dijit javascript object, which is the reason dijit.byId('alarmCatDialog').numberOfDateNode has a reference to your <input type='input' class='dateWidgetInput' .../>.

The main reason to use data-dojo-attach-point is that:

  • you can create multiple instances of dijit and therefore your template cannot identify nodes/dijits by IDs as you will have multiple nodes/dijits with the same ID
  • it's an elegant declarative way, so your code won't be full of dijit.byId/dojo.byId.

Upvotes: 9

Related Questions