Source for file AjaxDriver.php

Documentation is available at AjaxDriver.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Application
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../Object.php';
  24. 24:  
  25. 25: require_once dirname(__FILE__'/../Application/IAjaxDriver.php';
  26. 26:  
  27. 27:  
  28. 28:  
  29. 29: /**
  30. 30:  * AJAX output strategy.
  31. 31:  *
  32. 32:  * @author     David Grudl
  33. 33:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  34. 34:  * @package    Nette\Application
  35. 35:  */
  36. 36: class AjaxDriver extends Object implements IAjaxDriver
  37. 37: {
  38. 38:     /** @var array */
  39. 39:     private $data;
  40. 40:  
  41. 41:     /** @var IHttpResponse */
  42. 42:     private $httpResponse;
  43. 43:  
  44. 44:  
  45. 45:  
  46. 46:     /**
  47. 47:      * Generates link.
  48. 48:      * @param  string 
  49. 49:      * @return string 
  50. 50:      */
  51. 51:     public function link($url)
  52. 52:     {
  53. 53:         return "return !nette.action(" ($url === NULL "this.href" json_encode($url)) ", this)";
  54. 54:     }
  55. 55:  
  56. 56:  
  57. 57:  
  58. 58:     /**
  59. 59:      * @param  IHttpResponse 
  60. 60:      * @return void 
  61. 61:      */
  62. 62:     public function open(IHttpResponse $httpResponse)
  63. 63:     {
  64. 64:         $httpResponse->expire(FALSE);
  65. 65:         $this->httpResponse $httpResponse;
  66. 66:     }
  67. 67:  
  68. 68:  
  69. 69:  
  70. 70:     /**
  71. 71:      * @return void 
  72. 72:      */
  73. 73:     public function close()
  74. 74:     {
  75. 75:         if ($this->data === NULL{
  76. 76:             return// response already handled by user?
  77. 77:         }
  78. 78:  
  79. 79:         $this->httpResponse->setContentType('application/x-javascript''utf-8');
  80. 80:         echo json_encode($this->data);
  81. 81:         $this->data NULL;
  82. 82:     }
  83. 83:  
  84. 84:  
  85. 85:  
  86. 86:     /********************* AJAX response ****************d*g**/
  87. 87:  
  88. 88:  
  89. 89:  
  90. 90:     /**
  91. 91:      * Sets a response parameter. Do not call directly.
  92. 92:      * @param  string  name
  93. 93:      * @param  mixed   value
  94. 94:      * @return void 
  95. 95:      */
  96. 96:     public function __set($name$value)
  97. 97:     {
  98. 98:         $this->data[$name$value;
  99. 99:     }
  100. 100:  
  101. 101:  
  102. 102:  
  103. 103:     /**
  104. 104:      * Returns a response parameter. Do not call directly.
  105. 105:      * @param  string  name
  106. 106:      * @return mixed  value
  107. 107:      */
  108. 108:     public function &__get($name)
  109. 109:     {
  110. 110:         return $this->data[$name];
  111. 111:     }
  112. 112:  
  113. 113:  
  114. 114:  
  115. 115:     /**
  116. 116:      * Determines whether parameter is defined. Do not call directly.
  117. 117:      * @param  string    name
  118. 118:      * @return bool 
  119. 119:      */
  120. 120:     public function __isset($name)
  121. 121:     {
  122. 122:         return isset($this->data[$name]);
  123. 123:     }
  124. 124:  
  125. 125:  
  126. 126:  
  127. 127:     /**
  128. 128:      * Removes a response parameter. Do not call directly.
  129. 129:      * @param  string    name
  130. 130:      * @return void 
  131. 131:      */
  132. 132:     public function __unset($name)
  133. 133:     {
  134. 134:         unset($this->data[$name]);
  135. 135:     }
  136. 136:  
  137. 137:  
  138. 138:  
  139. 139:     /** @deprecated */
  140. 140:     public function fireEvent($event$arg)
  141. 141:     {
  142. 142:         trigger_error('Deprecated: use $presenter->ajaxDriver->events[] = array($event, $arg, ...); instead.'E_USER_WARNING);
  143. 143:         $args func_get_args();
  144. 144:         array_shift($args);
  145. 145:         $this->data['events'][array('event' => $event'args' => $args);
  146. 146:     }
  147. 147:  
  148. 148:  
  149. 149:  
  150. 150:     /** @deprecated */
  151. 151:     public function updateSnippet($id$content)
  152. 152:     {
  153. 153:         trigger_error('Deprecated: use $presenter->ajaxDriver->snippets[$id] = $content; instead.'E_USER_WARNING);
  154. 154:         $this->data['snippets'][$id= (string) $content;
  155. 155:     }
  156. 156: