_DomTemplated.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. define("dojox/dtl/_DomTemplated", [
  2. "dojo/dom-construct",
  3. ".",
  4. "./contrib/dijit",
  5. "./render/dom",
  6. "dojo/cache",
  7. "dijit/_TemplatedMixin"
  8. ], function(domConstruct,dtl,ddcd,ddrd,cache,TemplatedMixin){
  9. /*=====
  10. dtl = dojox.dtl;
  11. cache = dojo.cache;
  12. TemplatedMixin = dijit._TemplatedMixin
  13. =====*/
  14. dtl._DomTemplated = function(){};
  15. dtl._DomTemplated.prototype = {
  16. _dijitTemplateCompat: false,
  17. buildRendering: function(){
  18. // summary:
  19. // Construct the UI for this widget, setting this.domNode.
  20. //render needs a domNode to work with
  21. this.domNode = this.srcNodeRef;
  22. if(!this._render){
  23. var old = ddcd.widgetsInTemplate;
  24. ddcd.widgetsInTemplate = this.widgetsInTemplate;
  25. this.template = this.template || this._getCachedTemplate(this.templatePath, this.templateString);
  26. this._render = new ddrd.Render(this.domNode, this.template);
  27. ddcd.widgetsInTemplate = old;
  28. }
  29. var context = this._getContext();
  30. if(!this._created){
  31. delete context._getter;
  32. }
  33. this.render(context);
  34. this.domNode = this.template.getRootNode();
  35. if(this.srcNodeRef && this.srcNodeRef.parentNode){
  36. domConstruct.destroy(this.srcNodeRef);
  37. delete this.srcNodeRef;
  38. }
  39. },
  40. setTemplate: function(/*String|dojo._Url*/ template, /*dojox.dtl.Context?*/ context){
  41. // summary:
  42. // Quickly switch between templated by location
  43. // template: The new template.
  44. // context:
  45. // The runtime context.
  46. if(dojox.dtl.text._isTemplate(template)){
  47. this.template = this._getCachedTemplate(null, template);
  48. }else{
  49. this.template = this._getCachedTemplate(template);
  50. }
  51. this.render(context);
  52. },
  53. render: function(/*dojox.dtl.Context?*/ context, /*dojox.dtl.DomTemplate?*/ tpl){
  54. // summary:
  55. // Renders this template.
  56. // context:
  57. // The runtime context.
  58. // tpl:
  59. // The template to render. Optional.
  60. if(tpl){
  61. this.template = tpl;
  62. }
  63. this._render.render(this._getContext(context), this.template);
  64. },
  65. _getContext: function(context){
  66. if(!(context instanceof dojox.dtl.Context)){
  67. context = false;
  68. }
  69. context = context || new dojox.dtl.Context(this);
  70. context.setThis(this);
  71. return context;
  72. },
  73. _getCachedTemplate: function(templatePath, templateString){
  74. if(!this._templates){
  75. this._templates = {};
  76. }
  77. if(!templateString){
  78. templateString = cache(templatePath, {sanitize: true});
  79. }
  80. var key = templateString;
  81. var tmplts = this._templates;
  82. if(tmplts[key]){
  83. return tmplts[key];
  84. }
  85. return (tmplts[key] = new dojox.dtl.DomTemplate(
  86. TemplatedMixin.getCachedTemplate(
  87. templateString,
  88. true
  89. )
  90. ));
  91. }
  92. };
  93. return dojox.dtl._DomTemplated;
  94. });