Output.js 2.4 KB

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