Dojox/app: 是否可以通过编程方式定义视图
Dojox/app: Is it possible to define views programatically
是否可以通过编程方式而不是使用 html 模板来编写视图代码?我见过的所有恶魔都使用 html 模板。
是的,这是可能的。
添加您的 HTML 标记作为 属性 templateString
的字符串,
对于您的问题,以下代码不使用 .html 模板。
您可以使用字符串连接以编程方式修改模板。
有关 templateString here 的更多信息。
以下示例取自用户 Ben from this 对我的旧问题的回答:
require(['dijit/_WidgetBase', 'dijit/_TemplatedMixin', 'dojo/_base/declare', 'dojo/domReady!'], function(_WidgetBase, _TemplatedMixin, declare, domReady) {
//Foo represent any widget with template available in dojo
//replace by the widget you want to use
var Foo = declare([_WidgetBase, _TemplatedMixin], {});
var foo = new Foo({
templateString: '<div>This is my teemplate and ${bar}</div>',
bar: "this is added to the template"
});
foo.placeAt('layout');
});
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css" media="screen">
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<div id="layout"></div>
您还可以在 HTML 模板中使用占位符,在模板中定义基本标签结构并从 JS 端为占位符提供值。
例如:
<div>
<div data-dojo-type="dojox.layout.contentpane" >
{content}
</div>
</div>
或
您还可以根据 "postMixInProperties()"
动态修改某些 HTML 模板
请参阅小部件生命周期以获取相关信息
http://dojotoolkit.org/reference-guide/1.10/dijit/_WidgetBase.html
是否可以通过编程方式而不是使用 html 模板来编写视图代码?我见过的所有恶魔都使用 html 模板。
是的,这是可能的。
添加您的 HTML 标记作为 属性 templateString
的字符串,
对于您的问题,以下代码不使用 .html 模板。
您可以使用字符串连接以编程方式修改模板。
有关 templateString here 的更多信息。
以下示例取自用户 Ben from this
require(['dijit/_WidgetBase', 'dijit/_TemplatedMixin', 'dojo/_base/declare', 'dojo/domReady!'], function(_WidgetBase, _TemplatedMixin, declare, domReady) {
//Foo represent any widget with template available in dojo
//replace by the widget you want to use
var Foo = declare([_WidgetBase, _TemplatedMixin], {});
var foo = new Foo({
templateString: '<div>This is my teemplate and ${bar}</div>',
bar: "this is added to the template"
});
foo.placeAt('layout');
});
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css" media="screen">
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<div id="layout"></div>
您还可以在 HTML 模板中使用占位符,在模板中定义基本标签结构并从 JS 端为占位符提供值。
例如:
<div>
<div data-dojo-type="dojox.layout.contentpane" >
{content}
</div>
</div>
或
您还可以根据 "postMixInProperties()"
动态修改某些 HTML 模板请参阅小部件生命周期以获取相关信息 http://dojotoolkit.org/reference-guide/1.10/dijit/_WidgetBase.html