source: Dev/trunk/src/client/dojox/mvc/Output.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 3.0 KB
Line 
1define([
2        "dojo/_base/declare",
3        "dojo/_base/lang",
4        "dojo/dom",
5        "dijit/_WidgetBase",
6        "dojo/regexp"
7], function(declare, lang, dom, _WidgetBase, regexp){
8
9        return declare("dojox.mvc.Output", _WidgetBase, {
10                // summary:
11                //              A simple widget that displays templated output, parts of which may
12                //              be data-bound.
13                //
14                // description:
15                //              Simple output example:
16                //
17                //              |  <span data-dojo-type="dojox/mvc/Output" data-dojo-props="value: at(model, 'balance')"></span>
18                //
19                //              Another simple output example:
20                //
21                //              |  <span data-dojo-type="dojox/mvc/Output" data-dojo-props="value: at(model, 'balance')">
22                //              |    Your balance is: ${this.value}
23                //              |  </span>
24                //
25                //
26                //              The output widget being data-bound, if the balance changes in the
27                //              model, the content within the `<span>` will be
28                //              updated accordingly.
29
30                // exprchar:  Character
31                //              Character to use for a substitution expression, for a substitution string like ${this.value}
32                //              If this class is declared in a template HTML and exprchar is used in in-line template of this class, something other than `$` should be specified to avoid conflict with exprchar of outer-template.
33                exprchar: '$',
34       
35                // templateString: [private] String
36                //              The template or data-bound output content.
37                templateString : "",
38       
39                postscript: function(params, srcNodeRef){
40                        // summary:
41                        //              Override and save template from body.
42                        this.srcNodeRef = dom.byId(srcNodeRef);
43                        if(this.srcNodeRef){
44                                this.templateString = this.srcNodeRef.innerHTML;
45                                this.srcNodeRef.innerHTML = "";
46                        }
47                        this.inherited(arguments);
48                },
49       
50                set: function(name, value){
51                        // summary:
52                        //              Override and refresh output on value change.
53                        // name:
54                        //              The property to set.
55                        // value:
56                        //              The value to set in the property.
57                        this.inherited(arguments);
58                        if(name === "value"){
59                                this._output();
60                        }
61                },
62       
63                ////////////////////// PRIVATE METHODS ////////////////////////
64       
65                _updateBinding: function(name, old, current){
66                        // summary:
67                        //              Rebuild output UI if data binding changes.
68                        // tags:
69                        //              private
70                        this.inherited(arguments);
71                        this._output();
72                },
73       
74                _output: function(){
75                        // summary:
76                        //              Produce the data-bound output.
77                        // tags:
78                        //              private
79                        var outputNode = this.srcNodeRef || this.domNode;
80                        outputNode.innerHTML = this.templateString ? this._exprRepl(this.templateString) : this.value;
81                },
82       
83                _exprRepl: function(tmpl){
84                        // summary:
85                        //              Does substitution of ${foo+bar} type expressions in template string.
86                        // tags:
87                        //              private
88                        var pThis = this, transform = function(value, key){
89                                if(!value){return "";}
90                                var exp = value.substr(2);
91                                exp = exp.substr(0, exp.length - 1);
92                                with(pThis){
93                                        var val = eval(exp);
94                                        return (val || val == 0 ? val : "");
95                                }
96                        };
97                        transform = lang.hitch(this, transform);
98                        return tmpl.replace(new RegExp(regexp.escapeString(this.exprchar)+"(\{.*?\})","g"),
99                                function(match, key, format){
100                                        return transform(match, key).toString();
101                                });
102                }
103        });
104});
Note: See TracBrowser for help on using the repository browser.