[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 8 | * @class Ext.data.JsonWriter |
---|
| 9 | * @extends Ext.data.DataWriter |
---|
| 10 | * DataWriter extension for writing an array or single {@link Ext.data.Record} object(s) in preparation for executing a remote CRUD action. |
---|
| 11 | */ |
---|
| 12 | Ext.data.JsonWriter = Ext.extend(Ext.data.DataWriter, { |
---|
| 13 | /** |
---|
| 14 | * @cfg {Boolean} encode <p><tt>true</tt> to {@link Ext.util.JSON#encode JSON encode} the |
---|
| 15 | * {@link Ext.data.DataWriter#toHash hashed data} into a standard HTTP parameter named after this |
---|
| 16 | * Reader's <code>meta.root</code> property which, by default is imported from the associated Reader. Defaults to <tt>true</tt>.</p> |
---|
| 17 | * <p>If set to <code>false</code>, the hashed data is {@link Ext.util.JSON#encode JSON encoded}, along with |
---|
| 18 | * the associated {@link Ext.data.Store}'s {@link Ext.data.Store#baseParams baseParams}, into the POST body.</p> |
---|
| 19 | * <p>When using {@link Ext.data.DirectProxy}, set this to <tt>false</tt> since Ext.Direct.JsonProvider will perform |
---|
| 20 | * its own json-encoding. In addition, if you're using {@link Ext.data.HttpProxy}, setting to <tt>false</tt> |
---|
| 21 | * will cause HttpProxy to transmit data using the <b>jsonData</b> configuration-params of {@link Ext.Ajax#request} |
---|
| 22 | * instead of <b>params</b>.</p> |
---|
| 23 | * <p>When using a {@link Ext.data.Store#restful} Store, some serverside frameworks are |
---|
| 24 | * tuned to expect data through the jsonData mechanism. In those cases, one will want to set <b>encode: <tt>false</tt></b>, as in |
---|
| 25 | * let the lower-level connection object (eg: Ext.Ajax) do the encoding.</p> |
---|
| 26 | */ |
---|
| 27 | encode : true, |
---|
| 28 | /** |
---|
| 29 | * @cfg {Boolean} encodeDelete False to send only the id to the server on delete, true to encode it in an object |
---|
| 30 | * literal, eg: <pre><code> |
---|
| 31 | {id: 1} |
---|
| 32 | * </code></pre> Defaults to <tt>false</tt> |
---|
| 33 | */ |
---|
| 34 | encodeDelete: false, |
---|
| 35 | |
---|
| 36 | constructor : function(config){ |
---|
| 37 | Ext.data.JsonWriter.superclass.constructor.call(this, config); |
---|
| 38 | }, |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * <p>This method should not need to be called by application code, however it may be useful on occasion to |
---|
| 42 | * override it, or augment it with an {@link Function#createInterceptor interceptor} or {@link Function#createSequence sequence}.</p> |
---|
| 43 | * <p>The provided implementation encodes the serialized data representing the Store's modified Records into the Ajax request's |
---|
| 44 | * <code>params</code> according to the <code>{@link #encode}</code> setting.</p> |
---|
| 45 | * @param {Object} Ajax request params object to write into. |
---|
| 46 | * @param {Object} baseParams as defined by {@link Ext.data.Store#baseParams}. The baseParms must be encoded by the extending class, eg: {@link Ext.data.JsonWriter}, {@link Ext.data.XmlWriter}. |
---|
| 47 | * @param {Object/Object[]} data Data object representing the serialized modified records from the Store. May be either a single object, |
---|
| 48 | * or an Array of objects - user implementations must handle both cases. |
---|
| 49 | */ |
---|
| 50 | render : function(params, baseParams, data) { |
---|
| 51 | if (this.encode === true) { |
---|
| 52 | // Encode here now. |
---|
| 53 | Ext.apply(params, baseParams); |
---|
| 54 | params[this.meta.root] = Ext.encode(data); |
---|
| 55 | } else { |
---|
| 56 | // defer encoding for some other layer, probably in {@link Ext.Ajax#request}. Place everything into "jsonData" key. |
---|
| 57 | var jdata = Ext.apply({}, baseParams); |
---|
| 58 | jdata[this.meta.root] = data; |
---|
| 59 | params.jsonData = jdata; |
---|
| 60 | } |
---|
| 61 | }, |
---|
| 62 | /** |
---|
| 63 | * Implements abstract Ext.data.DataWriter#createRecord |
---|
| 64 | * @protected |
---|
| 65 | * @param {Ext.data.Record} rec |
---|
| 66 | * @return {Object} |
---|
| 67 | */ |
---|
| 68 | createRecord : function(rec) { |
---|
| 69 | return this.toHash(rec); |
---|
| 70 | }, |
---|
| 71 | /** |
---|
| 72 | * Implements abstract Ext.data.DataWriter#updateRecord |
---|
| 73 | * @protected |
---|
| 74 | * @param {Ext.data.Record} rec |
---|
| 75 | * @return {Object} |
---|
| 76 | */ |
---|
| 77 | updateRecord : function(rec) { |
---|
| 78 | return this.toHash(rec); |
---|
| 79 | |
---|
| 80 | }, |
---|
| 81 | /** |
---|
| 82 | * Implements abstract Ext.data.DataWriter#destroyRecord |
---|
| 83 | * @protected |
---|
| 84 | * @param {Ext.data.Record} rec |
---|
| 85 | * @return {Object} |
---|
| 86 | */ |
---|
| 87 | destroyRecord : function(rec){ |
---|
| 88 | if(this.encodeDelete){ |
---|
| 89 | var data = {}; |
---|
| 90 | data[this.meta.idProperty] = rec.id; |
---|
| 91 | return data; |
---|
| 92 | }else{ |
---|
| 93 | return rec.id; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | }); |
---|