4
<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>

如何显示我试过的这个对话框dijit.byId('alarmCatDialog').show();

上面的代码是一个模板,我dijit.byId('alarmCatDialog').show()从 .js 文件中调用。

dojo.attr(this.numberOfDateNode)这段代码有效,我得到了数据。但是如果我将 dojoattachpoint 更改为 id 那么我尝试dijit.byId('numberOfDateNode')将无法工作;

4

2 回答 2

9

您的numberOfDateNode是一个普通的 DOM 节点,而不是小部件/dijit,即 javascript 对象扩展dijit/_Widget,这就是您无法通过dijit.byId("numberOfDateNode"). 改用dojo.byId("numberOfDateNode"),你就准备好了。

dojoAttachPoint或其 HTML5 有效版本data-dojo-attach-pointdijit模板中用于附加对 DOM 节点或子 dijit 到 dijit javascript 对象的引用,这就是 dijit.byId('alarmCatDialog').numberOfDateNode引用您的<input type='input' class='dateWidgetInput' .../>.

使用的主要原因data-dojo-attach-point是:

  • 您可以创建多个dijit实例,因此您的模板无法通过 ID 识别节点/dijit,因为您将拥有多个具有相同 ID 的节点/dijit
  • 这是一种优雅的声明方式,因此您的代码不会充满dijit.byId/dojo.byId.
于 2012-06-16T00:39:10.253 回答
0

跟踪dijit.Dialog的内容模板非常重要。一旦你设置了对话框的内容,它的标记就会被解析——但不是以某种方式,这样 TemplatedMixin 就会应用于 content-markup-declared-widgets。

要成功实现模板,您将需要类似于以下代码的内容,请注意我已经评论了 attachPoints 的作用。

这个 SitePen 博客提供了关于这个主题的很好的信息

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;
        }

  });

});
于 2012-06-16T21:23:46.903 回答