【问题标题】:Looking up a specific widget by its id in the Dojo Toolkit在 Dojo Toolkit 中通过 id 查找特定小部件
【发布时间】:2012-09-04 09:11:44
【问题描述】:

我正在使用 Dojo Toolkit 开发一个移动应用程序。我有以下小部件:

define("js/custom/CustomHeading", [ "dojo/_base/declare", "dojo/dom-construct",
    "dojo/dom-class", "dojox/mobile/Heading", "dojox/mobile/ToolBarButton",
    "dojo/ready" ], function(declare, domConstruct, domClass, Heading,
    ToolBarButton, ready) {

return declare("js.custom.CustomHeading", [ Heading ], {

    buildRendering : function() {
        this.inherited(arguments);

        navigationButton = new dojox.mobile.ToolBarButton({
            icon : "js/custom/images/navigation.png"
        });

        navigationButton.placeAt(this.domNode);

        ready(function() {
            console.log("I entered the block.");
        });

    }
});

});

如果我尝试使用 dijit.byId() 在我的 html 中查找一个小部件,我不会得到成功的结果。 Dojo 找不到小部件,但它在那里。我要查找的小部件如下所示:

<div id="navigationOverlay" data-dojo-type="dojox.mobile.Overlay">
    <ul data-dojo-type="dojox.mobile.RoundRectList">
        <li data-dojo-type="dojox.mobile.ListItem"
            data-dojo-props="moveTo:'dataServerRuntime',transition:'slide',label:'Health Summary'"
            onclick="hideNavigation();"></li>
    </ul>
</div>

我做错了什么?如何找到小部件?

【问题讨论】:

  • 可能是时间问题 - 您是否检查过代码在 HTML 为 parsed 后是否运行?
  • 在我的data-dojo-config 属性中我使用parseOnLoad: true。这不是解决时间问题吗?如果没有,我如何防止我的代码在 DOM 加载之前运行?

标签: dojo


【解决方案1】:

使用dojo/ready:

ready(function() {
    var navigationWidget = reqistry.byId("navigationOverlay"); 
})

jsFiddle 的一个例子:http://jsfiddle.net/phusick/tgU2x/

【讨论】:

  • 这行得通,但我不能在我的声明块中使用它,我不知道为什么。如果我在ready(function() {}) 函数中写var navigationWidget = registry.byId("navigationOverlay");,则不会进入该块。在声明块之外它工作正常。
  • 你怎么知道没有进入区块?你尝试过console.log()alert() 吗?也许你在块中还有更多的东西,你需要以正确的分数执行块,即当this 指向小部件时,尝试:ready(this, function() { /*...*/ }) 或使用闭包。
  • 是的,我试过console.log()。我更新了上面的清单,向您展示了我现在拥有的东西。
  • 看看这个jsFiddle,它在那里工作得很好。
  • 它适用于另一个小部件,但不适用于js.widget.CustomHeading。在我的日志中,我看到了这一行 Uncaught Error: Tried to register widget with id==js_custom_CustomHeading_0 but that id is already registered。我认为这个错误是原因。你知道如何解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多