registry.byId for widget元素添加为dojo模板

时间:2014-09-15 23:17:00

标签: javascript dojo

我正在尝试访问已在窗口小部件中添加为模板的内容窗格,但我似乎可以使用registry.byId获取它:

我的模板 - 我正在尝试访问div“map”:

<div>

      <div id="map" 
           data-dojo-type="dijit/layout/ContentPane" 
           data-dojo-props="region:'center'" 
           style="padding:0;">im a map
         </div>

</div>

尝试访问“地图”的小部件代码

define([
  "dojo/_base/declare",
  "dijit/_WidgetBase",
  "dijit/_TemplatedMixin",
  "dojo/text!Templates/LandUse.htm",
   "dijit/registry",
    "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dijit/TitlePane",
], function (declare, _WidgetBase, _TemplatedMixin, LandUseTpl, registry) {
    return declare([_WidgetBase, _TemplatedMixin], {
        templateString: LandUseTpl,
        start: function () {//I've also tried postCreate but same result
            var Mymap = registry.byId("map");//this is always undefined
            if (Mymap) {
                alert("map found");
            }

        }
    });
});

实例化我的小部件并在我的主htm页面上调用start函数:

...
LU = new LandUse({}, "tool");
LU.start();

...

我怎么能抓住这个地图div以便我可以添加内容?

由于

1 个答案:

答案 0 :(得分:1)

您应该继承dijit/_WidgetsInTemplateMixin并使用data-dojo-attach-point属性,例如:

<div>
  <div id="map" 
    data-dojo-type="dijit/layout/ContentPane" 
    data-dojo-props="region:'center'" 
    data-dojo-attach-point="map"
    style="padding:0;">im a map
  </div>
</div>

如果您这样做了,您应该可以使用this.map访问您的小部件(类似于data-dojo-attach-point属性的名称)。

例如:http://plnkr.co/edit/1wBfAh3dwJxYBD1a3Eb6