簡單的模板使用lang.replace
根據的contentPane內容/模板的複雜性,一個簡單的方法來做到這一點是使用一個簡單的lang.replace
。比方說,你作出這樣的名字/年齡/生日模板(如cai/personTpl.html
):
<dl>
<dt>Name</dt><dd>{name.firstname} {name.lastname}</dd>
<dt>Age</dt><dd>{age}</dd>
<dt>Birthday</dt><dd>{birthday}</dd>
</dl>
在JavaScript,然後你可以這樣做:
require([..other deps.., 'dojo/_base/lang', 'dojo/text!cai/personTpl.html'],
function(..other deps.., lang, personTpl) {
//.. your other code ..
// assuming person[i] is something like:
// {name: {firstname: 'A', lastname: 'B'}, age: 42..}
var cp1 = new dijit.layout.ContentPane({
title:"New Question",
content: lang.replace(personTpl, person[i]),
});
dijit.byId("centerLayout").addChild(cp1);
}
);//~require
更多道場/ _base /郎::與此字典替換:使用自定義窗口小部件http://dojotoolkit.org/reference-guide/1.8/dojo/_base/lang.html#dojo-base-lang-replace
更復雜的模板
如果每個選項卡中使用的模板比較複雜,例如有自己的小部件,使用事件等,你最好寫一個自定義小部件(而不是使用ContentPane)。
例如,模板可以是這樣的(cai/widgets/personTpl.html
):
<dl>
<dt>Name</dt><dd data-dojo-attach-point='nameNode'></dd>
<dt>Age</dt><dd data-dojo-attach-point='ageNode'></dd>
<dt>Birthday</dt><dd>
<input type='text' data-dojo-type='dijit/form/DateTextBox'
data-dojo-attach-point='bdayInput' />
</dd>
</dl>
而且部件可以(cai/widgets/Person.js
):
define([ ..other deps.., "dojo/text!cai/widgets/personTpl.html"],
function(..other deps.., personTpl) {
return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
templateString: personTpl,
name: "unknown",
_setNameAttr: { node: "nameNode", type: "innerHTML" },
age: 0,
_setAgeAttr: { node: "ageNode", type: "innerHTML" },
birthday: new Date(),
_setBirthdayAttr: function(bday) {
this.bdayInput.set("value", bday);
this._set("birthday", bday);
}
}); //~declare
}
); //~define
然後,您可以添加的聯繫人小部件的實例TabContainer:
require([..other deps.., "cai/widgets/Person"],
function(..other deps.., Person) {
//.. your other code ..
// assuming person[i] is something like:
// {name: "Jerry", age: 42, birthday: new Date(), title: "JerryTab"}
var p = new Person(person[i]);
dijit.byId("centerLayout").addChild(p);
}
);//~require
更多關於widg ets(以及具有映射到節點的屬性的小部件):http://dojotoolkit.org/reference-guide/1.8/quickstart/writingWidgets.html#mapping-widget-attributes-to-domnode-attributes
感謝您的回覆,我會檢查您附加的鏈接,並要求您提供進一步的問題(如果有)。 – JerryCai 2013-02-20 09:28:38