Dojo 如何动态设置模板字符串?
在Dojo1.10+中,是否有一种方法可以动态设置基于模板的小部件的templateString 比如我试过这样的东西Dojo 如何动态设置模板字符串?,dojo,Dojo,在Dojo1.10+中,是否有一种方法可以动态设置基于模板的小部件的templateString 比如我试过这样的东西 ... return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], { templateString: undefined, constructor: function(myTemplate){ var that = this; //set the template
...
return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
templateString: undefined,
constructor: function(myTemplate){
var that = this;
//set the template
require(["dojo/text!" + myTemplate], function (template) {
that.templateString = template;
});
}
...
但它不成功,templateString始终未定义,因此它会因404错误而崩溃,因为它找不到html文件
这可能吗?如果只能设置有限的模板集,您可以使用以下方法:
define([
"dojo/_base/declare",
"dijit/_TemplatedMixin",
"dojo/text!path_to_template1.html",
"dojo/text!path_to_template2.html"
], function(declare, _TemplatedMixin, template1, template2)
{
return declare([_TemplatedMixin],
{
templateToUse: 1,
constructor: function(params)
{
this.templateToUse = params.templateToUse;
},
buildRendering: function()
{
switch(this.templateToUse) {
case 2:
this.templateString = template2;
break;
case 1:
this.templateString = template1;
break;
default:
this.templateString = template1;
};
this.inherited(arguments);
}
}
}
我不得不问这个用例是什么,因为我可以看到一些可能更好的替代方案
- 如果模板明显不同,为什么不创建单独的小部件?您可以使用dojo提供的OOP或mixin技术在它们之间共享功能
- 单个模板可以通过传递给小部件构造函数的参数进行高度定制。通过将“显示:无”样式设置为dojo附着点,可以将不同的图元附着到dojo附着点,甚至可以隐藏某些图元
define([
"dojo/_base/declare",
"dijit/_TemplatedMixin",
"dojo/text!path_to_template1.html",
"dojo/text!path_to_template2.html"
], function(declare, _TemplatedMixin, template1, template2)
{
return declare([_TemplatedMixin],
{
templateToUse: 1,
constructor: function(params)
{
this.templateToUse = params.templateToUse;
},
buildRendering: function()
{
switch(this.templateToUse) {
case 2:
this.templateString = template2;
break;
case 1:
this.templateString = template1;
break;
default:
this.templateString = template1;
};
this.inherited(arguments);
}
}
}
我不得不问这个用例是什么,因为我可以看到一些可能更好的替代方案
- 如果模板明显不同,为什么不创建单独的小部件?您可以使用dojo提供的OOP或mixin技术在它们之间共享功能
- 单个模板可以通过传递给小部件构造函数的参数进行高度定制。通过将“显示:无”样式设置为dojo附着点,可以将不同的图元附着到dojo附着点,甚至可以隐藏某些图元