1. #1
    Sencha User
    Join Date
    May 2010
    Posts
    172
    Vote Rating
    1
    Dmoney is on a distinguished road

      1  

    Default storeSync add record response doesn't update record ID ( I'm using 4.1 beta 3 )

    storeSync add record response doesn't update record ID ( I'm using 4.1 beta 3 )


    I'm using the MVC architecture and I'm trying to make sense of how store.sync() works now.

    I have a grid with records from the server and a form below it. the form is used for both editing and adding new records. When I add a new record I use the following in my controller

    Code:
        Ext.getCmp('myList').store.add(values);
        Ext.getCmp('myList').store.sync();
    this sends the new record to my php file and I enter it into the db. after the record is added I return all of the record info as json including the record ID which didn't exist before, it's an auto increment field in mysql.

    the record appears in the grid but isn't updated with the record id so if I make any more changes it just keeps creating duplicate records.

    shouldn't the record be updated with the recordID from the json I returned?

    how do I update the new record in the store to have the record ID that was returned from the server without reloading the whole store?

    here is what I'm returning after the insert: ("myDatabase" is the root declared in the reader for store contentID is the auto increment field and is also the idProperty in the store's model)

    Code:
    {"success":true,"resultText":"verified","myDatabase":[{"contentID":"35","title":"COPY - test","content":"some content text","date":"2012-03-11","sectionID":null,"imageKey":"","active":"n","link":"","masterID":null,"sequence":null}]}

  2. #2
    Sencha Premium Member vadimv's Avatar
    Join Date
    Sep 2010
    Location
    Cluj, Romania
    Posts
    654
    Vote Rating
    26
    vadimv will become famous soon enough vadimv will become famous soon enough

      0  

    Default


    for 4.0.7 the sync method replaces added new records with the records returned from the server, in onCreateRecords method, but seems that in 4.1 this method is a empty function, and the stores don't have implementation for it
    Code:
        // may be implemented by store subclasses
        onCreateRecords: Ext.emptyFn,
    Probably is needed to take it from 4.0.7 or to implement it yourself

  3. #3
    Sencha User
    Join Date
    May 2010
    Posts
    172
    Vote Rating
    1
    Dmoney is on a distinguished road

      0  

    Default


    thanks @vadimv,

    that explains a lot. I was going nuts trying to get that to work.

  4. #4
    Sencha User
    Join Date
    Dec 2011
    Posts
    224
    Vote Rating
    9
    dedoz will become famous soon enough

      0  

    Default


    you guys are missing the clientId in the response.
    while you have a store with phatom records (records that doesnt exists yet in the dabatabase) a store.sync sends them with a clientId, as at this moment he doesnt know their id it uses this to recognize them after you respond.
    so its like

    { newrecord : { id : unknown, name:'somename' }, clientId : 34534535 } ----> from client to server
    <--- from server to client { newrecord : { id:33, name:'somename' }, clientId : 34534535 }
    then Extjs know that record with id:33 references the phantom record with clientId 34534535
    so he says "now record with clientId 34534535 has id : 33'

    look at this
    Code:
        pStore = Ext.create('PersonStore');
        pStore.add(Ext.create('Person',{name:'peter'}));
        pStore.add(Ext.create('Person',{name:'mary'}));
        pStore.add(Ext.create('Person',{name:'alex'}));
        pStore.sync(); 
        
        // HTTP REQUEST
        //{"action":"testQuery","method":"personSelect",
        //    "data":[[
        //        {"someId":0,"name":"peter","clientId":"ext-record-1"},
        //        {"someId":0,"name":"mary","clientId":"ext-record-2"},
        //        {"someId":0,"name":"alex","clientId":"ext-record-3"}
        //]],"type":"rpc","tid":1}
        
        // RESPONSE
        // {"type":"rpc","tid":1,"action":"testQuery","method":"personInsert",
        //    "result":[
        //        {"id":"1","name":"peter","clientId":"ext-record-1"},
        //        {"id":"2","name":"mary","clientId":"ext-record-2"},
        //        {"id":"3","name":"alex","clientId":"ext-record-3"}
        //    ]}
    btw clientId is not a Person Model field , extjs add this to recognize them while goin to the server and coming back.
    so your response should include them, usually while creating new record, your server side function just should modify the model id field in the given request, and send back that, the same request just with the id modified.

  5. #5
    Sencha User
    Join Date
    May 2010
    Posts
    172
    Vote Rating
    1
    Dmoney is on a distinguished road

      0  

    Default


    That would make a lot of sense to me but that does not appear to be what is happening. I looked at the post data in firebug and there is no ClientID sent to the server.

  6. #6
    Sencha Premium Member vadimv's Avatar
    Join Date
    Sep 2010
    Location
    Cluj, Romania
    Posts
    654
    Vote Rating
    26
    vadimv will become famous soon enough vadimv will become famous soon enough

      0  

    Default


    yes, very probably, haven't tried, there's in documentation such config which says about. Seems that the diferences between 4.0.7 and 4.1 are bigger than I thought. Should catch it, but waiting for final release.

  7. #7
    Sencha Premium Member vadimv's Avatar
    Join Date
    Sep 2010
    Location
    Cluj, Romania
    Posts
    654
    Vote Rating
    26
    vadimv will become famous soon enough vadimv will become famous soon enough

      0  

    Default


    Quote Originally Posted by Dmoney View Post
    That would make a lot of sense to me but that does not appear to be what is happening. I looked at the post data in firebug and there is no ClientID sent to the server.
    have you tried to debug it ? and check if the record contains the clientIdProperty, I'm looking in code and you could try to step in "getRecordData: function(record, operation) " from Ext.data.writer.Writer, there's a piece of code:
    Code:
            if(isPhantom) {
                if(operation && operation.records.length > 1) {
                    // include clientId for phantom records, if multiple records are being written to the server in one operation.
                    // The server can then return the clientId with each record so the operation can match the server records with the client records
                    data[record.clientIdProperty] = record.internalId;
                }
            } else {
                // always include the id for non phantoms
                data[record.idProperty] = record.getId();
            }
    , before sending data to server, it is formated.

  8. #8
    Sencha User
    Join Date
    Dec 2011
    Posts
    224
    Vote Rating
    9
    dedoz will become famous soon enough

      0  

    Default


    post (in this forum) you data sent and data response and will check the problem (copy/paste from firebug)

  9. #9
    Sencha User
    Join Date
    Jul 2011
    Posts
    48
    Vote Rating
    1
    dcoan604 is on a distinguished road

      0  

    Default


    This is interesting. Recently I posted a question related to this.

    I was asking how, in 4.0.7, ExtJs matched up the data sent with the data returned. It seemed to me that ExtJS was relying on the fact that the Json response would always contain the same set of models, in exactly the same order as the request.

    I think that having an client-side ID sent and received is a good Idea. I DON'T really like the name "clientId" however.

    Also I think that this extjs-created ID property should Always be required... Not just on new records.

    If this were the case, you could then send a bunch of records to the server, and allow the server to process them in any way.... including allowing the server to CHANGE the primary ID key of a model.
    Also, the server could return less or more models when required. Models returned without a clientside ID (ie created on the server) could be added to the store on client side (and then given a clientId).
    Models sent from client (with clientId) but not returned by server, could be removed from the store.

    Just thinking...

  10. #10
    Sencha Premium Member vadimv's Avatar
    Join Date
    Sep 2010
    Location
    Cluj, Romania
    Posts
    654
    Vote Rating
    26
    vadimv will become famous soon enough vadimv will become famous soon enough

      0  

    Default


    Quote Originally Posted by dcoan604 View Post
    This is interesting. Recently I posted a question related to this.

    I was asking how, in 4.0.7, ExtJs matched up the data sent with the data returned. It seemed to me that ExtJS was relying on the fact that the Json response would always contain the same set of models, in exactly the same order as the request.
    right.
    Quote Originally Posted by dcoan604 View Post
    I think that having an client-side ID sent and received is a good Idea. I DON'T really like the name "clientId" however.
    yes, someone proposed at least "phantomId", or sth like this...