Hybrid View

  1. #1
    Ext User
    Join Date
    Jun 2008
    Location
    Dover, NH
    Posts
    10
    Vote Rating
    0
    iwtlf is on a distinguished road

      0  

    Default [FIXED][3.0rc1] cannot use onCreateRecord event to update pk

    [FIXED][3.0rc1] cannot use onCreateRecord event to update pk


    Hi,

    I need to add a record to a store and then [mostly] automatically update my pk based on the one generated by the server.

    It seems to me that this is what onCreateRecord is for but I haven't seen it documented anywhere so I'm not entirely certain what I'm doing wrong here. I'm not calling it directly as it is called automatically when I save the changes to the store. Here's the code:

    Code:
    var usersStore = new Ext.data.Store({
    	url: 'users',
    	reader: new Ext.data.JsonReader({
    		root: 'users',
    		successProperty: 'success',
    		id: 'key',
    		fields: [
    			{name: 'key'},
    			{name: 'nickname'}
    		]
    	}),
    	writer: new Ext.data.JsonWriter({writeAllFields:true}),
    	sortInfo:{field:'nickname', direction:'ASC'},
    	batchSave: true,
    	remoteSort: false
    });
    
    var userRecord = Ext.data.Record.create(['key','nickname']);
    
    var nRec = new userRecord({
    	nickname: 'test'
    });
    usersStore.add(nRec);
    usersStore.save(nRec);
    This part works fine and does send what I need to the server, which in turn does create the new record in the DB and assigns it an ID. Now I then output the following as the response (note: I have no idea what it is expecting so this was my best guess):

    Code:
    {"data": {"key": "somestring"}, "success": true}
    And this is what firebug gives me:

    Code:
    this.reader.meta.idProperty is undefined
    DomHelper()(Object phantom=true id=ext-record-1 data=Object, undefined)ext-all.js (line 1)
    DomHelper()(undefined, Object data=Object success=true, true)ext-all.js (line 1)
    DomHelper()(Object params=Object request=Object reader=Object, true, Object tId=3 status=200 statusText=OK)ext-all.js (line 1)
    b(Object tId=3 status=200 statusText=OK)ext-all.js (line 1)
    handleTransactionResponse()ext-base.js (line 104)
    handleReadyState()()ext-base.js (line 112)
    [Break on this error] Ext.DomHelper=function(){var w=null,l=/^...(Ext.History,new Ext.util.Observable());
    Any help will be appreciated thanks

  2. #2
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,496
    Vote Rating
    44
    Animal has a spectacular aura about Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    Don't you think the Store has a Record definition? Something it possibly derives from the fields config? See the API docs.

    Also, use idProperty instead of id

    http://extjs.com/playpen/docs/?class...ber=idProperty

    But also, you have to tell it its ID when you create it! http://extjs.com/playpen/docs/?class...&member=Record

  3. #3
    Sencha - Community Support Team mschwartz's Avatar
    Join Date
    Nov 2008
    Location
    San Diego, Peoples' Republic of California
    Posts
    2,053
    Vote Rating
    17
    mschwartz will become famous soon enough mschwartz will become famous soon enough

      0  

    Default


    Quote Originally Posted by Animal View Post
    But also, you have to tell it its ID when you create it! http://extjs.com/playpen/docs/?class...&member=Record
    Thanks for the link. Makes me want to upgrade to Ext 1.1 beta 2 now

  4. #4
    Ext User
    Join Date
    Jun 2008
    Location
    Dover, NH
    Posts
    10
    Vote Rating
    0
    iwtlf is on a distinguished road

      0  

    Default


    Thanks for the reply.

    I still don't fully get it though... I changed the code to that, which does work better:

    Code:
    var userRecord = Ext.data.Record.create(['nickname']);
    var usersStore = new Ext.data.Store({
    	url: 'users',
    	reader: new Ext.data.JsonReader({
    		root: 'users',
    		successProperty: 'success',
    		idProperty: 'key'
    	}),
    	writer: new Ext.data.JsonWriter({writeAllFields:true}),
    	sortInfo:{field:'nickname', direction:'ASC'},
    	batchSave: true,
    	remoteSort: false
    },userRecord);
    
    var nRec = new userRecord({
    	nickname: 'test'
    });
    usersStore.add(nRec);
    usersStore.save(nRec);
    I then return (I had already tried that format btw, but my propertyId was not set at the time):
    Code:
    {"users": [{"key": "somestring"}], "success": true}
    It now works fine except that it doesn't update the record's PK (and, logically so, keeps it dirty).

    But also, you have to tell it its ID when you create it!
    I'm not sure I understand that part though. If I do specify the ID when I create the record then it isn't marked as being phantom anymore and won't be sent to the server. Also, when I don't specify it, Ext will create a temporary unique key for me all the while marking the record as phantom, which is precisely the behavior I would expect pending a server update.
    When the server sends the data back with the actual PK, the onCreateRecord function does still have a reference to the record, and I would expect it to consume the JSON to update it and set the key.
    Is the returned JSON incorrectly formatted? What else am I misunderstanding here?

    Thanks.

  5. #5
    Ext User
    Join Date
    Jul 2007
    Location
    Florida
    Posts
    9,996
    Vote Rating
    5
    mjlecomte will become famous soon enough mjlecomte will become famous soon enough

      0  

    Default


    I'd set a breakpoint at this line and debug:
    Code:
    realize: function(rs, data){
        ...
            var values = this.extractValues(data, rs.fields.items, rs.fields.items.length);

  6. #6
    Ext User
    Join Date
    Jun 2008
    Location
    Dover, NH
    Posts
    10
    Vote Rating
    0
    iwtlf is on a distinguished road

      0  

    Default


    Ok so just to bring closure to this post.. this is the expected JSON string:
    Code:
    {"users": {"key": "somestring"}, "success": true}
    The thing that through me off is that I didn't know the reader could deal with something other than an array of objects in the root property, combined with the fact that when you create the record it expects just one object (directly) and will not accept an array of one.