Json3.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-data-writer-Json'>/**
  19. </span> * @class Ext.data.writer.Json
  20. This class is used to write {@link Ext.data.Model} data to the server in a JSON format.
  21. The {@link #allowSingle} configuration can be set to false to force the records to always be
  22. encoded in an array, even if there is only a single record being sent.
  23. * @markdown
  24. */
  25. Ext.define('Ext.data.writer.Json', {
  26. extend: 'Ext.data.writer.Writer',
  27. alternateClassName: 'Ext.data.JsonWriter',
  28. alias: 'writer.json',
  29. <span id='Ext-data-writer-Json-cfg-root'> /**
  30. </span> * @cfg {String} root The key under which the records in this Writer will be placed. Defaults to &lt;tt&gt;undefined&lt;/tt&gt;.
  31. * Example generated request, using root: 'records':
  32. &lt;pre&gt;&lt;code&gt;
  33. {'records': [{name: 'my record'}, {name: 'another record'}]}
  34. &lt;/code&gt;&lt;/pre&gt;
  35. */
  36. root: undefined,
  37. <span id='Ext-data-writer-Json-cfg-encode'> /**
  38. </span> * @cfg {Boolean} encode True to use Ext.encode() on the data before sending. Defaults to &lt;tt&gt;false&lt;/tt&gt;.
  39. * The encode option should only be set to true when a {@link #root} is defined, because the values will be
  40. * sent as part of the request parameters as opposed to a raw post. The root will be the name of the parameter
  41. * sent to the server.
  42. */
  43. encode: false,
  44. <span id='Ext-data-writer-Json-cfg-allowSingle'> /**
  45. </span> * @cfg {Boolean} allowSingle False to ensure that records are always wrapped in an array, even if there is only
  46. * one record being sent. When there is more than one record, they will always be encoded into an array.
  47. * Defaults to &lt;tt&gt;true&lt;/tt&gt;. Example:
  48. * &lt;pre&gt;&lt;code&gt;
  49. // with allowSingle: true
  50. &quot;root&quot;: {
  51. &quot;first&quot;: &quot;Mark&quot;,
  52. &quot;last&quot;: &quot;Corrigan&quot;
  53. }
  54. // with allowSingle: false
  55. &quot;root&quot;: [{
  56. &quot;first&quot;: &quot;Mark&quot;,
  57. &quot;last&quot;: &quot;Corrigan&quot;
  58. }]
  59. * &lt;/code&gt;&lt;/pre&gt;
  60. */
  61. allowSingle: true,
  62. //inherit docs
  63. writeRecords: function(request, data) {
  64. var root = this.root;
  65. if (this.allowSingle &amp;&amp; data.length == 1) {
  66. // convert to single object format
  67. data = data[0];
  68. }
  69. if (this.encode) {
  70. if (root) {
  71. // sending as a param, need to encode
  72. request.params[root] = Ext.encode(data);
  73. } else {
  74. //&lt;debug&gt;
  75. Ext.Error.raise('Must specify a root when using encode');
  76. //&lt;/debug&gt;
  77. }
  78. } else {
  79. // send as jsonData
  80. request.jsonData = request.jsonData || {};
  81. if (root) {
  82. request.jsonData[root] = data;
  83. } else {
  84. request.jsonData = data;
  85. }
  86. }
  87. return request;
  88. }
  89. });
  90. </pre>
  91. </body>
  92. </html>