2016-10-17 3 views
0

Dojo 1.10+에는 템플릿 기반 위젯의 templateString을 동적으로 설정하는 방법이 있습니까? 이 HTML 파일을 찾을 수 없기 때문에이 404 오류와 충돌 있도록 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; 
    }); 
} 
... 

처럼 뭔가를 시도했지만 실패했습니다.

이것도 가능합니까?

답변

2

이 같은 것을 사용할 수 있습니다 설정할 수 있습니다 템플릿의 제한된 세트가있는 경우 : 내가 몇 가지 대안을 볼 수 있기 때문에 이것에 대한 사용 사례가 무엇인지 물어 봐야

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 기술을 사용하여 두 기능을 공유 할 수 있습니다.
  • 위젯 생성자에 전달 된 매개 변수를 사용하여 단일 템플릿을 고도로 사용자 지정할 수 있습니다. 도저 부착 점에 다른 요소를 부착하거나 표시 : 없음 스타일을 설정하여 일부 요소를 숨길 수 있습니다.
+0

buildRendering을 호출 한 후 생성자에서 위젯으로 설정 한 속성이 null이된다는 것을 발견했습니다. buildRendering이 설명 할 수있는 템플릿을 설정하는 것 외에 다른 작업을 수행하고 있습니까? – erotavlas

+0

일반적으로 이러한 속성은 buildRendering이 호출 될 때 사용할 수 있어야합니다. 코드에 더 많은 부분을 게시 할 수 있다면 여전히 문제가있을 수 있습니다. – pgianna

+0

잘 나는 모든 코드를 생성자 (templateToUse 할당 제외)에서 포스트 생성자로 옮겼다. – erotavlas

관련 문제