简单的dijit小部件无法使用AMD编程进行初始化

时间:2012-07-19 08:16:03

标签: javascript dojo

我正在尝试使用dojo的新AMD功能完成简单的任务,并且我没有在屏幕上出现错误,结果也没有显示....

我想重写dojo参考指南中提到的第一个程序化示例dijit.Tree:

<script type="text/javascript">
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.Tree");

dojo.ready(function(){
  var store = new dojo.data.ItemFileReadStore({
      url: "{{dataUrl}}/dijit/tests/_data/countries.json"
  });

var treeModel = new dijit.tree.ForestStoreModel({
    store: store,
    query: {"type": "continent"},
    rootId: "root",
    rootLabel: "Continents",
    childrenAttrs: ["children"]
});

new dijit.Tree({
    model: treeModel
}, "treeOne");
});
</script>

到AMD版本:

<!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="js/dojo/dijit/themes/claro/claro.css" />
    <script src="js/dojo/dojo/dojo.js" data-dojo-config="async: true"></script>
    <script language="JavaScript">
    var param = new Array(null,["dojo/dom","dijit/tree","dojo/data/ItemFileReadStore","dijit/tree/ForestStoreModel","dojo/domReady!"]);
    require(param[1],function(dom,dtree,ifrs,fsm)
    {
        var store = new ifrs({url: "js/countries.json"});           
        var treeModel = new fsm({store: store,query: {"type": "continent"},rootId: "root",rootLabel: "Continents",childrenAttrs: ["children"]});

        new dtree({model:treeModel},"treeOne");
    });         
    </script>
</head>
<body class="claro">
<div id="treeOne">      
</div>
</body> 
</html>

Firebug没有向我发出运行时错误,或者没有告诉我某些东西丢失了。该页面保持空白。我做错了什么?!

1 个答案:

答案 0 :(得分:0)

主要问题是如何构建param数组并仅使用数组中的第一个值。

此外,dojo.readydojo/domReady!

不同
require(["dojo/dom", "dijit/tree", "dojo/data/ItemFileReadStore", 
  "dijit/tree/ForestStoreModel", "dojo/ready"],
function(dom, dtree, ifrs, fsm, ready) {     
  ready(function() {

    ...

  });
});