Fields: configure the field set used to reconfigure the Model before converting the response data to records.The Initialization Configuration of a Reader contains the following attributes ("fields" is generally included in the Model definition, so it is not displayed here ):
reader: { type : 'json', root : 'root', idProperty : 'id', totalProperty : 'total', successProperty: 'success', messageProperty: 'message'}
To obtain a response object that contains a different attribute defined in the above initialization, you need to use the 'metadata' attribute to dynamically reconfigure Reader. For example:
{ "count": 1, "ok": true, "msg": "Users found", "users": [{ "userId": 123, "name": "Ed Spencer", "email": "ed@sencha.com" }], "metaData": { "root": "users", "idProperty": 'userId', "totalProperty": 'count', "successProperty": 'ok', "messageProperty": 'msg' }}
You can place any data you need in the 'metadata' attribute, and Reader will ignore these attributes, however, you can use the metaData attribute of Reader (this attribute also listens to the Proxy 'smetachange event ). (Of course, it can also be replaced by store ). Then, the application code can process the obtained metadata in any way.
A simple example of how to use these to customize a suitable field set for the Model is a table (grid. By passing the 'fields' attribute, Reader automatically updates the Model internally, but these changes are not automatically reflected in the grid, unless you have updated the column configuration items. you can manually execute these operations, or use the standard grid configuration object column as the 'metadata' attribute, and then you can directly manage the grid. there is a very simple example to illustrate how to implement this situation:
// Response format :{... "metaData": {"fields": [{"name": "userId", "type": "int" },{ "name": "name ", "type": "string" },{ "name": "birthday", "type": "date", "dateFormat ": "Y-j-m"},], "columns": [{"text": "User ID", "dataIndex": "userId", "width ": 40 },{ "text": "User Name", "dataIndex": "name", "flex": 1 },{ "text": "Birthday ", "dataIndex": "birthday", "flex": 1, "format": 'Y-j-m', "xtype": "datecolumn"}]}
Reader can automatically read the configuration of the meta field set and recreate the Model based on the new field set. However, to process the new column configuration, you must process the metadata in the application code. The metadata processing time can be easily passed through store or proxy, for example:
var store = Ext.create('Ext.data.Store', { ... listeners: { 'metachange': function(store, meta) { myGrid.reconfigure(store, meta.columns); } } });
Ext. data. reader. XmlXML Reader is used by the proxy to read responses in the XML format returned by the server. This usually happens in the result of loading the Store. For example, we create a similar:
Ext.define('User', { extend: 'Ext.data.Model', fields: ['id', 'name', 'email']});var store = Ext.create('Ext.data.Store', { model: 'User', proxy: { type: 'ajax', url : 'users.xml', reader: { type: 'xml', record: 'user', root: 'users' } }});
The above example creates a 'user' Model. If you are not familiar with the Model, refer to the Model documentation for detailed explanations.
Notify Store's {@ linkExt. data. proxy. proxy} We need an XML Reader to create a very simple XML Reader. Store will automatically pass the model parameters to Store, so we can pass parameters in the following way instead:
reader: { type : 'xml', model: 'User', record: 'user', root: 'users'}
The reader we set above is ready to read data from the server-at this time, reader will receive the following response:
1
Ed Spencer
ed@sencha.com
2
Abe Elias
abe@sencha.com
First, the root option is used to define the root node.
(A complete XML document must have only one root ). XML Reader then uses the configuration item record to pull the data into each record-in this example, we set the record to 'user', so each of the above
Will be converted into objects in the User model.
Note: XmlReader does not care whether the root node root and record elements are nested in a larger structure, so the following response data can still work effectively:
1
Ed Spencer
ed@sencha.com
2
Abe Elias
abe@sencha.com
Response metadataThe server can return additional data in the response, such as the number of Record Sets and response success status. They are generally included in the XML response as follows:
100
true
1
Ed Spencer
ed@sencha.com
2
Abe Elias
abe@sencha.com
If these attributes exist in the XML response, they can be parsed and loaded by XmlReader to the Store. We can set the names of these attributes by specifying the last pair of configuration items:
reader: { type: 'xml', root: 'users', totalProperty : 'total', successProperty: 'success'}
These final configuration items are not required for Reader creation, but can also be useful. When the server needs to report an error or indicate that a large amount of data is available, only one subset is returned.
Response formatNote: In order for the browser to parse the returned XML document, the Content-Type header in the HTTP response must be set to "text/xml" or "application/xml ". This is very important-otherwise XmlReader will not work properly