Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXT JS Store's Proxy: readers and writers

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.

like image 796
Muzaaya Joshua Avatar asked Dec 16 '22 06:12

Muzaaya Joshua


1 Answers

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');
        }
    }
});
like image 74
dbrin Avatar answered Dec 24 '22 11:12

dbrin