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