从架构层次上来说,dijit位于dojo之上,“是Dojo中界面控件的总称,他们使用灵活而且易于理解。每个Dijit控件都是由Dojo类以及控件中使用的图片,CSS等资源文件共同组成。通过Dijit架构,可以方便的创建灵活、可扩展、可定制外观的控件”。(引用认识Dojo中的界面控件:Dijit )
<script>
require(["dojo/parser", "dijit/registry", "dojo/dom", "dojo/domReady!"], function(parser, registry, dom){
parser.parse();
// dom.byId("foobar") would only be a normal domNode.
var myDialog = registry.byId("foobar");
myDialog.set("content", "<p>I've been replaced!</p>");
myDialog.show();
});
</script>
<div data-dojo-type="dijit/Dialog" id="foobar" title="Foo!">
<p>I am some content</p>
</div>
如果要获取对用于展示部件的真实dom节点的引用,由于dijit将这个引用当作一个属性存储在部件的.domNode中,因此可以使用部件示例的.domNode属性获取这个引用。可以使用.domNode这个属性来设置节点的样式、位置及其它dom操作。
require(["dijit/registry", "dojo/dom-construct", "dojo/_base/window"], function(registry, domConstruct, win){
var thinger = registry.byId("foobar");
domConstruct.place(thinger.domNode, win.body(), "last");
// functionally equivalent to:
// win.body().appendChild(thinger.domNode);
});
在以编程方式创建部件时,要传递一个id参数:
require(["dijit/Dialog", "dijit/registry"], function(Dialog, registry){
var myDialog = new Dialog({
id: "myDialog",
title: "Programmatic"
});
myDialog.startup();
// compare them:
console.log(registry.byId("myDialog") == myDialog);
});
否则,dijit会自动生成一个标识符ID。
require(["dijit/Dialog", "dijit/registry"], function(Dialog, registry){
var myDialog = new Dialog({ title:"No ID" })
console.log(myDialog.get("id"));
});
所有的dijit都遵循相同的编程惯例。使用javascript的new函数创建一个实例,以哈希的方式传递属性和方法,提供源节点的引用(可选)。
require(["dijit/Dialog", "dojo/dom"], function(Dialog, dom){
var node = dom.byId("makeADialog");
var myDialog = new Dialog({ title:"From Source Node" }, node);
myDialog.show();
});
// set title
myTitlePane.set('title', 'hello world');
// find out if button is disabled
var dis = myButton.get('disabled');
// set to the current date
myDateTextBox.set('value', new Date());
Set() 也支持类似dojo/dom-style样式的哈希表,一次设置多个属性。
myTitlePane.watch("open", function(attr, oldVal, newVal){
console.log("pane is now " + (newVal ? "opened" : "closed"));
});
require(["dojo/dom-style", "dijit/registry"], function(domStyle, registry){
// hide a widget with id="myThinger"
domStyle.set(registry.byId("myThinger").domNode, "display", "none");
});
require(["dijit/Dialog"], function(Dialog){
var myDialog = new Dialog({ title:"foo" }, "bar");
myDialog.declaredClass == "dijit/Dialog" // true
});
require(["dijit/form/Button"], function(Button){
new Button({
label: 'Click me!',
onClick: function(evt){ console.log("clicked!"); }
});
});
事件处理函数可以采用编程的方式(像上面),也可以使用声明的方式,如下:
<div data-dojo-type="dijit/form/Button">
<script type="dojo/on" data-dojo-event="click" data-dojo-args="evt">
console.log("clicked, event object is ", evt);
</script>
Click me!
</div>
<div id="myDialog" data-dojo-type="dijit/Dialog" title="A Dialog"><p class="innerContent">Content</p>/div>
registry.byId("myDialog").show(); // show my dialog instance
如果没有id,使用registy.getEnclosingWidget函数传递任何一个对子dom节点的引用,会返回对部件的引用。
require(["dojo/query", "dijit/registry"], function(query, registry){
var node = query("p.innerContent")[0]; // a domNode found by query
var w = registry.getEnclosingWidget(node); // find the widget this node is in
w.show();
});
最后一种方法,也是最常用的方法,与getEnclosingWidget类似,但是只有当被传递的节点是部件的domNode的成员时才可用。
require(["dijit/form/Button"], function(Button){
var myButton = new Button({label: "press me"});
});
上面的代码创建了一个部件,在html文档中myButton.domNode相应的位置。当以声明的方式初始化时,如下:
<button data-dojo-type="dijit/form/Button">press me</button>
当查看 节点的属性设置和innerHTML后,原始的button节点会被删除,新建的DOM自动替换旧的button节点。
require(["dojox/widget/FishEyeLite"], function(FishEyeLite){
new FishEyeLite({...}, "mySourceDom");
});
dijit没有任何behavioral 部件。