Results 1 to 3 of 3

Thread: How to get the record index on adding a record in the grid store with proxy(REST)

  1. #1
    Sencha User
    Join Date
    Dec 2013
    Location
    India
    Posts
    18

    Default Answered: How to get the record index on adding a record in the grid store with proxy(REST)

    Hi,
    I am adding a sample code which I use , where adding a row doesn't provide a record index in proxy post request for me. As I am using a remote store , I need the record index in post request for row insert to maintain the order in remote.

    Code:
    Ext.onReady(function(){    console.log("ext ready");
        Ext.define('User', {
             extend: 'Ext.data.Model',
             fields: ['firstName','lastName','age','eyeColor'],
             proxy: {
                type: 'rest',
                url : '/ajax',
                reader: {
                    type: 'json',
                    rootProperty: 'data',
                    successProperty: 'success'
                },
                writer: {
                    type: 'json',
                    rootProperty: 'data'
                }
            }
         });
    
    
        Ext.create('Ext.data.Store', {
            storeId: 'store1',
            model: 'User',
            pageSize: 10,
            autoSync:true,
            data : [],
        });
    
    
        Ext.create('Ext.grid.Panel', {
            title: 'Data Table',
            store: Ext.data.StoreManager.lookup('store1'),
            columns: [
                { text: 'First Name', dataIndex: 'firstName', flex:1, editor:{xtype:'textfield'}},
                { text: 'Last Name', dataIndex: 'lastName', editor:'textfield' }
        
            ],
            selModel: 'cellmodel',
            plugins:['cellediting'],
            //bufferedRenderer:false,
            height: 400,
            
            width: 600,
            tbar:[{   
                        xtype: 'button', 
                        text: 'Load',
                        handler: function(){
                            var store = Ext.data.StoreManager.lookup('store1');
                            store.load({params:{
                                            start: 0,
                                            limit: 2
                                            }
                                        });
                        }
                    },
                    {   
                        xtype: 'button', 
                        text: 'save',
                        handler: function(){
                        var store = Ext.data.StoreManager.lookup('store1');
                        store.sync();
                        }
                    },
                    {   
                        xtype: 'button', 
                        text: 'Add user @2',
                        handler: function(){
                        var user = Ext.create('User', {firstName: 'Ed', lastName: 'Spencer'});
                        //user.save();
                        var store = Ext.data.StoreManager.lookup('store1');
                        //store.addSorted(user);
                        store.add(user);
                        //store.insert(2, user);
                        }
                    },
                    {   
                        xtype: 'button', 
                        text: 'Update user',
                        handler: function(){
                        var store = Ext.data.StoreManager.lookup('store1');
                        var user = store.getAt(2);
                        user.set('lastName', 'zing');
                        //user.save();
                        user.save(
                            {
                                failure: function (record, operation) {
                                    record.reject();
                                    console.log("callback is called");
                                }
                            }
                        );
                        }
                    },
                    {   
                        xtype: 'button', 
                        text: 'Delete user',
                        handler: function(){
                            var store = Ext.data.StoreManager.lookup('store1');
                            var user = store.getAt(0);
                            user.erase(
                                        {
                                            failure: function (record, operation) {
                                                //record.reject();
                                                console.log("callback is called");
                                            }
                                        }
                            );
                        }
                    }
                ],
            align:'stretch',
            renderTo: Ext.getBody()
        });
    }
    );

    Original Request:
    POST - {'data': {'lastName': 'Spencer', 'id': 'User-14', 'firstName': 'Ed'}}

    Expected Request:
    POST - {'data': {'lastName': 'Spencer', 'id': 'User-14', 'firstName': 'Ed' }, 'recordIndex':0}
    Last edited by yuvabarath; 15 Feb 2016 at 10:41 PM. Reason: Adding the request

  2. You can use 'tranform' property of writer:
    Code:
    Ext.define('User', {        extend: 'Ext.data.Model',
            fields: ['firstName', 'lastName', 'age', 'eyeColor'],
            proxy: {
                type: 'rest',
                url: '/ajax',
                reader: {
                    type: 'json',
                    rootProperty: 'data',
                    successProperty: 'success'
                },
                writer: {
                    type: 'json',
                    rootProperty: 'data',
                    // SOMETHING LIKE THIS
                    transform: {
                        fn: function(data, request) {
                            data.recordIndex = Ext.data.StoreManager.lookup('store1').indexOf(data);
                            console.log(data);
                            return data;
                        },
                        scope: this
                    }
                }
            }
        });

  3. #2
    Sencha User yeghikyan's Avatar
    Join Date
    Mar 2009
    Location
    München
    Posts
    574
    Answers
    99

    Default

    You can use 'tranform' property of writer:
    Code:
    Ext.define('User', {        extend: 'Ext.data.Model',
            fields: ['firstName', 'lastName', 'age', 'eyeColor'],
            proxy: {
                type: 'rest',
                url: '/ajax',
                reader: {
                    type: 'json',
                    rootProperty: 'data',
                    successProperty: 'success'
                },
                writer: {
                    type: 'json',
                    rootProperty: 'data',
                    // SOMETHING LIKE THIS
                    transform: {
                        fn: function(data, request) {
                            data.recordIndex = Ext.data.StoreManager.lookup('store1').indexOf(data);
                            console.log(data);
                            return data;
                        },
                        scope: this
                    }
                }
            }
        });
    "The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague." – Edsger W. Dijkstra

  4. #3
    Sencha User
    Join Date
    Dec 2013
    Location
    India
    Posts
    18

    Default

    Thanks Yeghikyan,
    This is very helpful.

    I was expecting a config to add this info, because store has this information already. If many records are modified, this transformation has to happen for all the data which will decrease the performance.if it was done from at proxy/sync it will be optimal.


Similar Threads

  1. Replies: 2
    Last Post: 19 Feb 2014, 11:52 PM
  2. Replies: 1
    Last Post: 2 Oct 2013, 10:10 AM
  3. [FIXED] Unable to destroy a record of a REST proxy-based store
    By totolitoto in forum Sencha Touch 2.x: Bugs
    Replies: 5
    Last Post: 8 Jun 2012, 8:38 AM
  4. Replies: 3
    Last Post: 19 Apr 2012, 11:51 AM
  5. Replies: 4
    Last Post: 10 Aug 2011, 2:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •