_DomTemplated.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.dtl._DomTemplated"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.dtl._DomTemplated"] = true;
  8. dojo.provide("dojox.dtl._DomTemplated");
  9. dojo.require("dijit._Templated");
  10. dojo.require("dojox.dtl.dom");
  11. dojo.require("dojox.dtl.render.dom");
  12. dojo.require("dojox.dtl.contrib.dijit");
  13. dojox.dtl._DomTemplated = function(){};
  14. dojox.dtl._DomTemplated.prototype = {
  15. _dijitTemplateCompat: false,
  16. buildRendering: function(){
  17. // summary:
  18. // Construct the UI for this widget, setting this.domNode.
  19. //render needs a domNode to work with
  20. this.domNode = this.srcNodeRef;
  21. if(!this._render){
  22. var ddcd = dojox.dtl.contrib.dijit;
  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 dojox.dtl.render.dom.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. dojo.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. if(dojox.dtl.text._isTemplate(template)){
  44. this.template = this._getCachedTemplate(null, template);
  45. }else{
  46. this.template = this._getCachedTemplate(template);
  47. }
  48. this.render(context);
  49. },
  50. render: function(/*dojox.dtl.Context?*/ context, /*dojox.dtl.DomTemplate?*/ tpl){
  51. if(tpl){
  52. this.template = tpl;
  53. }
  54. this._render.render(this._getContext(context), this.template);
  55. },
  56. _getContext: function(context){
  57. if (!(context instanceof dojox.dtl.Context)) {
  58. context = false;
  59. }
  60. context = context || new dojox.dtl.Context(this);
  61. context.setThis(this);
  62. return context;
  63. },
  64. _getCachedTemplate: function(templatePath, templateString){
  65. if(!this._templates){
  66. this._templates = {};
  67. }
  68. var key = templateString || templatePath.toString();
  69. var tmplts = this._templates;
  70. if(tmplts[key]){
  71. return tmplts[key];
  72. }
  73. return (tmplts[key] = new dojox.dtl.DomTemplate(
  74. dijit._Templated.getCachedTemplate(
  75. templatePath,
  76. templateString,
  77. true
  78. )
  79. ));
  80. }
  81. };
  82. }