Context.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.dtl.Context"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.dtl.Context"] = true;
  8. dojo.provide("dojox.dtl.Context");
  9. dojo.require("dojox.dtl._base");
  10. dojox.dtl.Context = dojo.extend(function(dict){
  11. this._this = {};
  12. dojox.dtl._Context.call(this, dict);
  13. }, dojox.dtl._Context.prototype,
  14. {
  15. getKeys: function(){
  16. var keys = [];
  17. for(var key in this){
  18. if(this.hasOwnProperty(key) && key != "_this"){
  19. keys.push(key);
  20. }
  21. }
  22. return keys;
  23. },
  24. extend: function(/*dojox.dtl.Context|Object*/ obj){
  25. // summary: Returns a clone of this context object, with the items from the
  26. // passed objecct mixed in.
  27. return dojo.delegate(this, obj);
  28. },
  29. filter: function(/*dojox.dtl.Context|Object|String...*/ filter){
  30. // summary: Returns a clone of this context, only containing the items
  31. // defined in the filter.
  32. var context = new dojox.dtl.Context();
  33. var keys = [];
  34. var i, arg;
  35. if(filter instanceof dojox.dtl.Context){
  36. keys = filter.getKeys();
  37. }else if(typeof filter == "object"){
  38. for(var key in filter){
  39. keys.push(key);
  40. }
  41. }else{
  42. for(i = 0; arg = arguments[i]; i++){
  43. if(typeof arg == "string"){
  44. keys.push(arg);
  45. }
  46. }
  47. }
  48. for(i = 0, key; key = keys[i]; i++){
  49. context[key] = this[key];
  50. }
  51. return context;
  52. },
  53. setThis: function(/*Object*/ _this){
  54. this._this = _this;
  55. },
  56. getThis: function(){
  57. return this._this;
  58. },
  59. hasKey: function(key){
  60. if(this._getter){
  61. var got = this._getter(key);
  62. if(typeof got != "undefined"){
  63. return true;
  64. }
  65. }
  66. if(typeof this[key] != "undefined"){
  67. return true;
  68. }
  69. return false;
  70. }
  71. });
  72. }