1 | define([ |
---|
2 | "dojo/_base/declare", |
---|
3 | "dojo/_base/lang", |
---|
4 | "dojo/dom", |
---|
5 | "dijit/_WidgetBase" |
---|
6 | ], function(declare, lang, dom, _WidgetBase){ |
---|
7 | /*===== |
---|
8 | declare = dojo.declare; |
---|
9 | dom = dojo.dom; |
---|
10 | _WidgetBase = dijit._WidgetBase; |
---|
11 | =====*/ |
---|
12 | |
---|
13 | return declare("dojox.mvc.Output", [_WidgetBase], { |
---|
14 | // summary: |
---|
15 | // A simple widget that displays templated output, parts of which may |
---|
16 | // be data-bound. |
---|
17 | // |
---|
18 | // description: |
---|
19 | // Simple output example: |
---|
20 | // |
---|
21 | // | <span dojoType="dojox.mvc.Output" ref="model.balance"> |
---|
22 | // | Your balance is: ${this.value} |
---|
23 | // | </span> |
---|
24 | // |
---|
25 | // The output widget being data-bound, if the balance changes in the |
---|
26 | // dojox.mvc.StatefulModel, the content within the <span> will be |
---|
27 | // updated accordingly. |
---|
28 | |
---|
29 | // templateString: [private] String |
---|
30 | // The template or data-bound output content. |
---|
31 | templateString : "", |
---|
32 | |
---|
33 | postscript: function(params, srcNodeRef){ |
---|
34 | // summary: |
---|
35 | // Override and save template from body. |
---|
36 | this.srcNodeRef = dom.byId(srcNodeRef); |
---|
37 | if(this.srcNodeRef){ |
---|
38 | this.templateString = this.srcNodeRef.innerHTML; |
---|
39 | this.srcNodeRef.innerHTML = ""; |
---|
40 | } |
---|
41 | this.inherited(arguments); |
---|
42 | }, |
---|
43 | |
---|
44 | set: function(name, value){ |
---|
45 | // summary: |
---|
46 | // Override and refresh output on value change. |
---|
47 | this.inherited(arguments); |
---|
48 | if(name === "value"){ |
---|
49 | this._output(); |
---|
50 | } |
---|
51 | }, |
---|
52 | |
---|
53 | ////////////////////// PRIVATE METHODS //////////////////////// |
---|
54 | |
---|
55 | _updateBinding: function(name, old, current){ |
---|
56 | // summary: |
---|
57 | // Rebuild output UI if data binding changes. |
---|
58 | // tags: |
---|
59 | // private |
---|
60 | this.inherited(arguments); |
---|
61 | this._output(); |
---|
62 | }, |
---|
63 | |
---|
64 | _output: function(){ |
---|
65 | // summary: |
---|
66 | // Produce the data-bound output. |
---|
67 | // tags: |
---|
68 | // private |
---|
69 | var outputNode = this.srcNodeRef || this.domNode; |
---|
70 | outputNode.innerHTML = this.templateString ? this._exprRepl(this.templateString) : this.value; |
---|
71 | }, |
---|
72 | |
---|
73 | _exprRepl: function(tmpl){ |
---|
74 | // summary: |
---|
75 | // Does substitution of ${foo+bar} type expressions in template string. |
---|
76 | // tags: |
---|
77 | // private |
---|
78 | var pThis = this, transform = function(value, key){ |
---|
79 | if(!value){return "";} |
---|
80 | var exp = value.substr(2); |
---|
81 | exp = exp.substr(0, exp.length - 1); |
---|
82 | with(pThis){return eval(exp) || "";} |
---|
83 | }; |
---|
84 | transform = lang.hitch(this, transform); |
---|
85 | return tmpl.replace(/\$\{.*?\}/g, |
---|
86 | function(match, key, format){ |
---|
87 | return transform(match, key).toString(); |
---|
88 | }); |
---|
89 | } |
---|
90 | }); |
---|
91 | }); |
---|