1. #1
    Sencha User
    Join Date
    Sep 2012
    Posts
    65
    Answers
    7
    Vote Rating
    1
    warrean is on a distinguished road

      0  

    Default Unanswered: How to refactor Ext.Ajax.request to Ext.Model.save

    Unanswered: How to refactor Ext.Ajax.request to Ext.Model.save


    Hi,

    I have the following code:
    Code:
    assignModel: function(model, date, callback) {
       var credentials = Ext.getStore('credentialsStore').first();
    
       Ext.Ajax.request({
                url: MyApp.Save,
                params: {
                    username: credentials.get('username'),
                    password: "",
                    token: credentials.get('token'),
                    ApiKey: MyApp.apiKey,
                    date: Ext.Date.format(date, 'Y-m-d'),
                    modelId: model.get('id')
                },
                success: function () {
                    Ext.callback(callback.success);
                },
                failure: function () {
                    Ext.callback(callback.failure, null, ["Failed to assign model"]);
                }
            });
    }
    And I want to refactor this by using model.save, here's the model:
    Code:
    Ext.define('MyApp.Model', {    extend: 'Ext.data.Model',
        config: {
            idProperty: 'id',
            fields: [
                {
                    name: 'id',
                    mapping: 'Id',
                    type: 'string'
                },
                {
                    name: 'name',
                    mapping: 'Name',
                    type: 'string'
                }
            ],
            proxy: {
                type: 'rest',
                url : MyApp.Save,
                actionMethods: {
                    create : 'POST',
                    update : 'POST',
                }
            }
        }
    });
    When I do the first method, I get a clean POST-request with all the params.

    When I do the model.save() method, I get an OPTION and a POST-request with the JSON-object somewhere in it.

    How can I alter the save method from the model, so it sends the (username, password, token,...) parameters too? Why are there 2 requests being sent?

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,404
    Answers
    3549
    Vote Rating
    850
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    If you get an OPTIONS request first, that is the browser checking the server to see if it supports CORS.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

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

  3. #3
    Sencha User
    Join Date
    Sep 2012
    Posts
    65
    Answers
    7
    Vote Rating
    1
    warrean is on a distinguished road

      0  

    Default


    I see. But the request is still being sent wrong, I sent a JSON-object now with model.save() but I need to send a request like the one in the picture.

    Most importantly: I don't get the username, token, apiKey parameters which are necessary for the back-end-request.

    This is what the server expects:
    Attachment 40644

    So what do I have to do for the model.save() to work like this?

  4. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,404
    Answers
    3549
    Vote Rating
    850
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    when you do a model.save() are you passing in the params or have them as extraParams on the proxy?
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    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 in print!

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

  5. #5
    Sencha User
    Join Date
    Sep 2012
    Posts
    65
    Answers
    7
    Vote Rating
    1
    warrean is on a distinguished road

      0  

    Default


    I now have them as extraParams, however they get sent in the queryString. Here's how the model.save() request looks:

    Screen Shot 2012-12-07 at 16.43.23.png

    So they're not in the "Request Payload", and there's no "Form Data"-entry with the Model-parameters. It's just the JSON-object, and the server isn't expecting that.

Thread Participants: 1