_Container.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. define("dojox/mvc/_Container", [
  2. "dojo/_base/declare",
  3. "dojo/_base/lang",
  4. "dijit/_WidgetBase",
  5. "dojo/regexp"
  6. ], function(declare, lang, _WidgetBase, regexp){
  7. /*=====
  8. declare = dojo.declare;
  9. _WidgetBase = dijit._WidgetBase;
  10. =====*/
  11. return declare("dojox.mvc._Container", [_WidgetBase], {
  12. // stopParser: [private] Boolean
  13. // Flag to parser to not try and parse widgets declared inside the container.
  14. stopParser: true,
  15. // exprchar: Character
  16. // Character to use for a substitution expression, for a substitution string like ${this.index}
  17. exprchar: '$',
  18. // templateString: [private] String
  19. // The template or content for this container. It is usually obtained from the
  20. // body of the container and may be modified or repeated over a collection/array.
  21. // In this simple implementation, attach points, attach events and WAI
  22. // attributes are not supported in the template.
  23. templateString : "",
  24. // _containedWidgets: [protected] dijit._Widget[]
  25. // The array of contained widgets at any given point in time within this container.
  26. _containedWidgets : [],
  27. ////////////////////// PROTECTED METHODS ////////////////////////
  28. _parser : null,
  29. _createBody: function(){
  30. // summary:
  31. // Parse the body of this MVC container widget.
  32. // description:
  33. // The bodies of MVC containers may be model-bound views generated dynamically.
  34. // Parse the body, start an contained widgets and attach template nodes for
  35. // contained widgets as necessary.
  36. // tags:
  37. // protected
  38. if(!this._parser){
  39. try{
  40. // returns dojo/parser if loaded, otherwise throws
  41. this._parser = require("dojo/parser");
  42. }catch(e){
  43. // if here, dojo/parser not loaded
  44. try{
  45. // returns dojox/mobile/parser if loaded, otherwise throws
  46. this._parser = require("dojox/mobile/parser");
  47. }catch(e){
  48. // if here, both dojox/mobile/parser and dojo/parser are not loaded
  49. console.error("Add explicit require(['dojo/parser']) or explicit require(['dojox/mobile/parser']), one of the parsers is required!");
  50. }
  51. }
  52. }
  53. if(this._parser){
  54. this._containedWidgets = this._parser.parse(this.srcNodeRef,{
  55. template: true,
  56. inherited: {dir: this.dir, lang: this.lang},
  57. propsThis: this,
  58. scope: "dojo"
  59. });
  60. }
  61. },
  62. _destroyBody: function(){
  63. // summary:
  64. // Destroy the body of this MVC container widget. Also destroys any
  65. // contained widgets.
  66. // tags:
  67. // protected
  68. if(this._containedWidgets && this._containedWidgets.length > 0){
  69. for(var n = this._containedWidgets.length - 1; n > -1; n--){
  70. var w = this._containedWidgets[n];
  71. if(w && !w._destroyed && w.destroy){
  72. w.destroy();
  73. }
  74. }
  75. }
  76. },
  77. ////////////////////// PRIVATE METHODS ////////////////////////
  78. _exprRepl: function(tmpl){
  79. // summary:
  80. // Does substitution of ${foo+bar} type expressions in template string.
  81. // tags:
  82. // private
  83. var pThis = this, transform = function(value, key){
  84. if(!value){return "";}
  85. var exp = value.substr(2);
  86. exp = exp.substr(0, exp.length - 1);
  87. with(pThis){return eval(exp);}
  88. };
  89. transform = lang.hitch(this, transform);
  90. return tmpl.replace(new RegExp(regexp.escapeString(this.exprchar)+"(\{.*?\})","g"),
  91. function(match, key, format){
  92. return transform(match, key).toString();
  93. });
  94. }
  95. });
  96. });