EXT JS Store's Proxy: readers and writers

16,828

Solution 1

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');
        }
    }
});

Solution 2

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

Share:
16,828

Related videos on Youtube

Muzaaya Joshua
Author by

Muzaaya Joshua

Software/Telecommunications engineer. Spend my free time enjoying Erlang, NoSQL (CouchDB, RIAK, Couchbase e.t.c.), Web Sockets, JavaScript, HTML5, Python3.x, and the other main-stream world of Java, Oracle, MySQL e.t.c.

Updated on June 04, 2022

Comments

  • Muzaaya Joshua
    Muzaaya Joshua almost 2 years

    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.

  • Ben Orozco
    Ben Orozco over 9 years
    "Write" event is not fired when autosync=false. Is this the expected behavior or is it bug?
  • Ben Orozco
    Ben Orozco over 9 years
    Fixed: As I'm ussing a Session, store.sync() is needed to trigger the event