4
votes

In the documentation, i have found a store instantiated like this:

var store = Ext.create('Ext.data.Store', {
    autoLoad: true,
    model: "User",
    proxy: {
        type: 'ajax',
        url : 'users.json',
        reader: {
            type: 'json',
            root: 'users'
        }
    }
});

The proxy has one url config. I am particularly interested in the reader. The reader specifies the data exchange format (json) and the root ('users'). Now, in other words if the store is set up to be: autoLoad = true, then EXT JS will make an Ajax connection to the url specified in order to read. Now, how would i configure a writer for that same store above? Someone also tell me about this: if i configure a writer, would it use the same url as specified in the proxy? am still confused about writers and readers in context of the code i have showed above, you would help me use the above example to show readers and writer configs. Thank you.

2

2 Answers

10
votes

Here is an example of a store with reader, writer and api in my App:

Ext.define('MyApp.store.Tasks', {
    extend: 'Ext.data.Store',
    model: 'MyApp.model.Task',
    sorters : [{
       property: 'idx',
       direction: 'ASC'
    }],
    autoSync:true,
    proxy:{
        type: 'ajax',
        reader: {
            type: 'json',
            root: 'data'
        },
        writer: {
            type: 'json',
            writeAllFields : false,  //just send changed fields
            allowSingle :false      //always wrap in an array
           // nameProperty: 'mapping'
       },
        api: {
               // read:
                create: 'task/bulkCreate.json',
                update: 'task/bulkUpdate.json'
               // destroy:
        }
    },
    listeners : {
        write: function(store, operation, opts){
            console.log('wrote!');
            //workaround to sync up store records with just completed operation
            Ext.each(operation.records, function(record){
                if (record.dirty) {
                    record.commit();
                }
            });
        },
        update:function(){
            console.log('tasks store updated');
        }
    }
});
2
votes

Actually you are correct - it will use the same url as for reader.

Proxy is a mediator between your model/store on a client and your server code on another side. Readers are used for reading data and you could configure stuff like formatting, specify root etc. Writers are in charge of save/update requests to the server.

Check this article: http://edspencer.net/2011/02/proxies-extjs-4.html