1. #1
    Sencha User
    Join Date
    Jul 2012
    Location
    Italy
    Posts
    84
    Vote Rating
    4
    Answers
    1
    Tegola is on a distinguished road

      0  

    Default Unanswered: Different rootProperty for each action (create/read/update/destroy)

    Unanswered: Different rootProperty for each action (create/read/update/destroy)


    Hello again! I'm sorry, I'm on a request spree today

    I'm dealing with a rest service that returns data with "messages" as the rootProperty when loading multiple items (/messages), but returns "message" when working with a single item (/messages/<id>).

    If I create a new item using the model object (record.save()), I need it to do a POST with a rootProperty "message". The server then returns the record, and Sencha updates the model object with the id specified by the server. So far, so good.

    However, when adding the above record in a store (store.add(record)), the store's sync method will do a POST to the server with a rootProperty "message", which is right, but will not read the data returned by the server because the store reader expects "messages", while the server returns "message" instead.

    Is there a way to specify different rootProperty for each action? Or, how do you solve this issue? Keep in mind that I cannot change how the server returns the data.

    Thanks in advance

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,714
    Vote Rating
    436
    Answers
    3113
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    You can change the rootProperty value on the writer before you save or load or sync.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Jul 2012
    Location
    Italy
    Posts
    84
    Vote Rating
    4
    Answers
    1
    Tegola is on a distinguished road

      0  

    Default


    Doesn't seem to work, I don't get why. The rootProperty is changed but the record is not updated with the server generated id.

    Here's how I did it.

    Reader:
    Code:
    Ext.define('My.JsonReader', {
        extend: 'Ext.data.JsonReader',
        alias : 'reader.myreader',
        
        config: {
            rootProperties: null
        }
    });
    Proxy:
    Code:
    Ext.define('My.Proxy', {
        extend: 'Ext.data.proxy.Rest',
        alias: 'proxy.myproxy',
        
        doRequest: function(operation, callback, scope){
            var reader = this.getReader();
            var roots = reader._rootProperties;
            var action = operation.getAction();
            
            if (roots !== undefined && roots !== null && roots !== false && roots[action]) {
                reader.setRootProperty(roots[action]);
            }
            
            this.callParent(arguments);
        }
    });
    Store:
    Code:
    Ext.define('My.MessagesStore', {
        extend: 'Ext.data.Store',
        
        config: {
            model: 'My.Message',
            
            proxy: {
                type: 'myproxy',
                url: '/messages',
                reader: {
                    type: 'myreader',
                    rootProperties: {
                        create: 'message',
                        read: 'messages',
                        update: 'message',
                        destroy: 'message'
                    }
                },
                writer: {
                    type: 'json',
                    rootProperty: 'message'
                }
            }
        }
    });
    Help?

  4. #4
    Sencha User
    Join Date
    Jul 2012
    Location
    Italy
    Posts
    84
    Vote Rating
    4
    Answers
    1
    Tegola is on a distinguished road

      0  

    Default


    I forgot to add: the store is set to autoSync, and the record is saved successfully, but launching store.sync() will make the store save it again.

Tags for this Thread