1. #1
    Sencha User
    Join Date
    Feb 2012
    Location
    Davenport, IA
    Posts
    30
    Vote Rating
    6
    Answers
    2
    WillFM is on a distinguished road

      2  

    Default Answered: JSON Proxy, create multiple record sync issue.

    Answered: JSON Proxy, create multiple record sync issue.


    I'm having an issue with creating multiple new records in a store (extjs 4.1), and syncing them with the server.

    When I send the create request, I'm returning an identical array, with the newly assigned id from the server appended into the id property, but the id does not want to propagate back to the store. It works fine with single Items, that aren't when there's multiple items in the store to be created in the sync:

    Example:
    PHP Code:
    /***Works***/
    //Request:
    {
      
    Data: {"id":"","text":"Foo bar"}
    }

    //Response:
    {
      
    Data: {"id":"1","text":"Foo bar"},
      
    "success"true
    }



    /***Doesn't work***/
    //Request:
    {
      
    Data: [
        {
    "id":"","text":"Foo bar"},
        {
    "id":"","text":"Bar foo"}
      ]
    }
    //Response:
    {
      
    Data: [
        {
    "id":"1","text":"Foo bar"},
        {
    "id":"2","text":"Bar foo"}
      ],
      
    "success"true

    Any clue what I could be doing wrong? does it expect it in a different format then above?

  2. @WillFM,

    It seems that you are using Ext 4.1. If this is the case and you want to create multiple records, you should config clientIdProperty for your model. Ex:
    Code:
    Ext.define('MyModel', {
        clientIdProperty: 'clientId',
        ....
    })
    Then when you sync with server, each record in the request will automatically include an client id as below:
    Code:
    {
      Data: [
        {"id":"","text":"Foo bar", "clientId":"ext-record-XX"},
        {"id":"","text":"Bar foo", "clientId":"ext-record-YY"}
      ]
    }
    Your returned data from server should include client id for each record to update the client side.
    Code:
    {
      Data: [
        {"id":"1","text":"Foo bar", "clientId":"ext-record-XX"},
        {"id":"2","text":"Bar foo", "clientId":"ext-record-YY"}
      ],
      "success": true
    }

  3. #2
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,197
    Vote Rating
    482
    Answers
    690
    scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future

      -1  

    Default


    This is not possible.
    You will need to issue an ajax request to send the data and then call store.load() to see the changes.

    Regards,
    Scott.

  4. #3
    Sencha User
    Join Date
    Feb 2012
    Location
    Davenport, IA
    Posts
    30
    Vote Rating
    6
    Answers
    2
    WillFM is on a distinguished road

      0  

    Default


    Scott, thank you for the reply.

    Just to make sure you understand the question, the example I gave, (response ways), is the JSON, that ext generates itself.

    I don't understand why it would be designed to update when a single record is sync'd on a store, but would not do the same, when several items, were sent. I would think this would defeat the purpose, of the store sync command, to sync all changes to the store with the database, and vise versa .

    What would be your suggestion then to update these, I would think your store load(); suggestion, would leave the old records in tact, and then in case return "duplicate" records. sans's the previously created records lacking the Id's I'm trying to obtain from the server. I guess I could just search the store for records after the sync, that don't have an id. remove them, and then do the load(). the issue with that, is then the old records, would be marked as removed, and would want to destroy() on the next time I called sync(). Would there be away to delete these without making the store dirty?

  5. #4
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,197
    Vote Rating
    482
    Answers
    690
    scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future

      -1  

    Default


    Updating multiple is fine, see:
    http://dev.sencha.com/deploy/ext-4.1...er/writer.html

    If you edit a record that has off id it will simulate an error. If you then edit an id with an even id, it will update both as expected. You can see the response.

    The issue you are addressing is new records ... All the sent id's are "" and the server returns them as 1,2 ...

    Scott.

  6. #5
    Sencha - Community Support Team
    Join Date
    Jan 2012
    Posts
    1,376
    Vote Rating
    115
    Answers
    346
    vietits is a splendid one to behold vietits is a splendid one to behold vietits is a splendid one to behold vietits is a splendid one to behold vietits is a splendid one to behold vietits is a splendid one to behold

      2  

    Default


    @WillFM,

    It seems that you are using Ext 4.1. If this is the case and you want to create multiple records, you should config clientIdProperty for your model. Ex:
    Code:
    Ext.define('MyModel', {
        clientIdProperty: 'clientId',
        ....
    })
    Then when you sync with server, each record in the request will automatically include an client id as below:
    Code:
    {
      Data: [
        {"id":"","text":"Foo bar", "clientId":"ext-record-XX"},
        {"id":"","text":"Bar foo", "clientId":"ext-record-YY"}
      ]
    }
    Your returned data from server should include client id for each record to update the client side.
    Code:
    {
      Data: [
        {"id":"1","text":"Foo bar", "clientId":"ext-record-XX"},
        {"id":"2","text":"Bar foo", "clientId":"ext-record-YY"}
      ],
      "success": true
    }

  7. #6
    Sencha User Izhaki's Avatar
    Join Date
    Apr 2009
    Location
    London
    Posts
    118
    Vote Rating
    13
    Answers
    7
    Izhaki will become famous soon enough

      0  

    Default


    Quote Originally Posted by vietits View Post
    @WillFM,

    It seems that you are using Ext 4.1. If this is the case and you want to create multiple records, you should config clientIdProperty for your model.
    @vietits, I cannot thank you enough for this reply. I have been debugging the framework for 4 hours and honestly wouldn't know what to do had I not found this thread.

  8. #7
    Sencha User conorarmstrong's Avatar
    Join Date
    Mar 2008
    Location
    Northern Ireland
    Posts
    224
    Vote Rating
    2
    conorarmstrong is on a distinguished road

      0  

    Default


    I think it is important to note here that even if the clientIdProperty config is set, then the phantom id is ONLY sent if more than one record is sent to the server. If only a single record is sent, then the phantom id is not sent. Server side code will have to deal with this correctly.

    Spent the past hour pulling my hair out looking for the property in the JSON sent to the server when only saving one record. You need to save two or more to see it.
    ------------------------------------------
    Conor Armstrong
    tw: @evathedog
    web: rockstown.com

    Ext.ux.form.AutoCombo
    Ext.ux.SimpleIFrame
    Ext.ux.form.ToolFieldSet

    Knowledge is realising that the street is one-way, wisdom is looking both directions anyway.