Extjs4 - is there a non json / xml script for proxy?

I am creating some models to interact with an existing API from a previous project.

The API uses standard POST methods to save data.

I configured the model and proxies to such an extent that they push data to the server, but there seem to be only two types of entries: json and xml.

proxy: { /* ... */ reader: { type: 'json', root: 'results' }, writer: { type: '???' // <-- can only see json or xml in the docs } } 

Is there a standard POST writer that simply passes data in message fields?

I am surprised that this will not be a standard record type.

(Parsing the json format will not be too difficult to implement, but it will mean updating a large number of old api files.)

+8
extjs extjs4
source share
4 answers

Well, I was able to easily create this author by checking the source code of existing writers.

One thing that existing writers can do - and perhaps that is why the development team only implemented the json and xml versions - is that they can click multiple entries at the same time.

This can be implemented in POST, but it will be a bit more complicated.

This script will work if you are trying to push one model on the api using POST:

 Ext.define('Ext.data.writer.SinglePost', { extend: 'Ext.data.writer.Writer', alternateClassName: 'Ext.data.SinglePostWriter', alias: 'writer.singlepost', writeRecords: function(request, data) { request.params = data[0]; return request; } }); 

and use this to write to the proxy:

 writer: { type: 'singlepost' } 
+5
source share

Based on Ben's answer, I implemented my own script that will collect all the properties of all models into arrays. For example, if you have a model with some fields:

 fields:[ {name:'id', type:'int'} {name:'name', type:'string'} {name:'age', type:'date'} ] 

The query string will be

 id=1&id=2&id=...&name=oleks&name=max&name=...&age=... 

the code:

 Ext.define('Ext.data.writer.SinglePost', { extend: 'Ext.data.writer.Writer', alternateClassName: 'Ext.data.SinglePostWriter', alias: 'writer.singlepost', writeRecords: function(request, data) { if(data && data[0]){ var keys = []; for(var key in data[0]){ keys.push(key); } for(var i=0;i<keys.length;i++){ request.params[keys[i]] = []; for(var j=0;j<data.length;j++){ request.params[keys[i]].push((data[j])[keys[i]]); } } } return request; } }); 
+1
source share

For Sencha touch 2.0, change the writeRecords method to:

 writeRecords: function (request, data) { var params = request.getParams() || {}; Ext.apply(params, data[0]); request.setParams(params); return request; } 
+1
source share

Here is my version adapted from the answers above:

 // Subclass the original XmlWriter Ext.define('MyApp.utils.data.writer.XmlInAPostParameter', { extend : 'Ext.data.writer.Xml', // give it an alias to use in writer 'type' property alias : 'writer.xml_in_a_post_parameter', // override the original method writeRecords : function(request, data) { // call the overriden method - it will put the data that I // want into request.xmlData this.callParent(arguments); // copy the data in request.xmlData. In this case the XML // data will always be in the parameter called 'XML' Ext.apply(request.params, { XML: request.xmlData }); // Already copied the request payload and will not send it, // so we delete it from the request delete request.xmlData; // return the modified request object return request; } }); Ext.define("MyApp.model.MyModel", { extend : "Ext.data.Model", requires : [ 'MyApp.utils.data.writer.XmlInAPostParameter' ], fields : [ 'field_A', 'field_B' ], proxy : { type : 'ajax', api : { read : '/mymodel/read.whatever', update : '/mymodel/write.whatever' }, reader : { type : 'xml' }, writer : { // use the alias we registered before type : 'xml_in_a_post_parameter' } } }); 
0
source share