使用dijit.byId w dojox.mobile小部件

时间:2012-07-08 16:39:21

标签: dojo dojox.mobile

我正在通过此函数在静态定义的dojox.mobile.RoundRectList小部件下动态构建一系列dojox.mobile.ListItem小部件...

function displayOpps(items) {

// Create the list container that will hold application names
var rrlOppsContainer = dijit.byId("rrlOpps");   
// Add a new item to the list container for each element in the server respond
for (var i in items){

    // Create and populate the list container with applications' names
    var name = items[i].CustName + " - " + items[i].OppNbr;
    var liOpps = new dojox.mobile.ListItem({
        label: name,
        moveTo: "sv3OppDetail"  
    });

    // Add the newly created item to the list container
    rrlOppsContainer.addChild(liOpps);
}

}

当我在html文件中的onLoad()期间运行此代码时,使用Chrome的开发工具我得到以下错误...

未捕获的TypeError:对象#没有方法'byId'

我已经阅读了很多围绕这个主题的文章,似乎很多人都有这个问题,但我发现的每一个都与其他技术有关(例如,Spring MVC等),我试图使用它基于dojox.mobile的应用程序。也就是说,我尝试通过将其包含在我的html文件中来模仿其他人提出的一些解决方案,但它仍然不起作用......

<script type="text/javascript"
data-dojo-config="isDebug: true, async: true, parseOnLoad: true"
src="dojo/dojo.js">
dojo.require("dojox.mobile.RoundRectList")
</script>

我做错了什么?

提前感谢您的时间和专业知识。

1 个答案:

答案 0 :(得分:3)

如果您使用的是Dojo 1.7+,您可能只是忘记了需要“dijit / registry”模块。这是定义byId函数的地方。当您使用桌面小部件时,这是由其他基本模块间接加载的,但是使用dojox / mobile,您必须明确加载它(因为默认情况下dojox / mobile仅加载一组非常小的模块,以最大限度地减少代码占用)。

根据您编写应用程序的方式,执行以下操作:

dojo.require("dijit.registry");  // legacy (pre-1.7) loader syntax
...
var rrlOppsContainer = dijit.byId("rrlOpps");
...

或者这个:

require(["dijit/registry", ...], function(registry, ...){ // 1.7+ AMD loader syntax
    ...
    var rrlOppsContainer = registry.byId("rrlOpps");
    ...
});


另请注意,第二个代码示例在使用旧版加载器语法时尝试使用异步加载(async:true)。这不起作用,要获得异步加载,必须使用AMD语法。

相关问题