Dojo 如何动态设置模板字符串?

Dojo 如何动态设置模板字符串?,dojo,Dojo,在Dojo1.10+中,是否有一种方法可以动态设置基于模板的小部件的templateString 比如我试过这样的东西 ... return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], { templateString: undefined, constructor: function(myTemplate){ var that = this; //set the template

在Dojo1.10+中,是否有一种方法可以动态设置基于模板的小部件的templateString

比如我试过这样的东西

...
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附着点,甚至可以隐藏某些图元

调用buildRendering后,我发现我从构造函数在小部件上设置的属性变为null,buildRendering除了设置可能解释这一点的模板外,是否还做了其他事情?通常,调用buildRendering时,这些属性应该可用。如果您可以发布更多部分的代码,这将非常有用,如果您仍然对此有问题的话。我将所有代码(除了TemplateUse赋值)从构造函数移动到post create,并修复了它。在调用buildRendering后,我发现我从构造函数在小部件上设置的属性变为null,除了设置可能解释这一点的模板之外,buildRendering还做了其他事情吗?通常在调用buildRendering时,这些属性应该是可用的。如果您可以发布更多的代码部分,这将非常有用,如果您仍然对此有问题的话。我将我的所有代码(模板使用赋值除外)从构造函数移动到post create,这就解决了它。