Context.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. define("dojox/dtl/Context", [
  2. "dojo/_base/lang",
  3. "./_base"
  4. ], function(lang,dd){
  5. /*=====
  6. dd = dojox.dtl;
  7. =====*/
  8. /*=====
  9. dd.Context = function(dict){
  10. // summary: Represents a runtime context used by DTL templates.
  11. }
  12. =====*/
  13. dd.Context = lang.extend(function(dict){
  14. this._this = {};
  15. dd._Context.call(this, dict);
  16. }, dd._Context.prototype,
  17. {
  18. getKeys: function(){
  19. // summary: Returns the set of keys exported by this context.
  20. var keys = [];
  21. for(var key in this){
  22. if(this.hasOwnProperty(key) && key != "_this"){
  23. keys.push(key);
  24. }
  25. }
  26. return keys;
  27. },
  28. extend: function(/*dojox.dtl.Context|Object*/ obj){
  29. // summary: Returns a clone of this context object, with the items from the
  30. // passed objecct mixed in.
  31. return lang.delegate(this, obj);
  32. },
  33. filter: function(/*dojox.dtl.Context|Object|String...*/ filter){
  34. // summary: Returns a clone of this context, only containing the items
  35. // defined in the filter.
  36. var context = new dd.Context();
  37. var keys = [];
  38. var i, arg;
  39. if(filter instanceof dd.Context){
  40. keys = filter.getKeys();
  41. }else if(typeof filter == "object"){
  42. for(var key in filter){
  43. keys.push(key);
  44. }
  45. }else{
  46. for(i = 0; arg = arguments[i]; i++){
  47. if(typeof arg == "string"){
  48. keys.push(arg);
  49. }
  50. }
  51. }
  52. for(i = 0, key; key = keys[i]; i++){
  53. context[key] = this[key];
  54. }
  55. return context;
  56. },
  57. setThis: function(/*Object*/ _this){
  58. // summary: Sets the object on which to perform operations.
  59. // _this: the this ref.
  60. this._this = _this;
  61. },
  62. getThis: function(){
  63. // summary: Gets the object on which to perform operations.
  64. return this._this;
  65. },
  66. hasKey: function(/*String*/key){
  67. // summary: Indicates whether the specified key is defined on this context.
  68. // key: The key to look up.
  69. if(this._getter){
  70. var got = this._getter(key);
  71. if(typeof got != "undefined"){
  72. return true;
  73. }
  74. }
  75. if(typeof this[key] != "undefined"){
  76. return true;
  77. }
  78. return false;
  79. }
  80. });
  81. return dojox.dtl.Context;
  82. });