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,483
    Vote Rating
    35
    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.

  7. #7
    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 don't know if my pointer is the one that tipped you off or not, but if you did break in that area I believe it "should" handle an array, and if it does not, it "might" be a bug. If you debug with the original source files you can see the commentary that seems to indicate it should process an array.

  8. #8
    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


    Looks like it may expect the type of the data returned to match the type sent out. So if you create one record, you need to send one record back. If you create multiple records, then you'd send an array back.

    This area of the code has been evolving. Maybe the data type will be standardized/fixed to just take an array to simplify things.

  9. #9
    Ext JS Premium Member christocracy's Avatar
    Join Date
    Oct 2006
    Location
    Montreal
    Posts
    380
    Vote Rating
    0
    christocracy is on a distinguished road

      0  

    Default


    For the actions "create", "destroy" and "save", the response data must be of the same form as the request.

    If you create, destroy or save 1 record, send 1 record back.
    If you create, destroy or save x records, send x records back in an array.

    Further, when receiving x records to create, destroy or save, the records must be returned in the same order received (currently).

    However, I'm sure I can re-tool the DataReader to pick out the single record if you send back an Array of 1 record.
    /**
    * @author Chris Scott
    * @business www.transistorsoft.com
    * @rate $120USD / hr; training $500USD / day / developer (5 dev min)
    *
    * @SenchaDevs http://senchadevs.com/developers/transistor-software
    * @twitter http://twitter.com/#!/christocracy
    * @github https://github.com/christocracy
    */

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

      0  

    Default


    [to mjlecompte] I'd looked at the code but not debugged it since it originally didn't get into the function at all (I had the wrong meta id property set). Your post prompted me to spend more time with the debugger now that the other issue was fixed.

    [to christocracy] I think it'll be useful to expand a bit on the documentation in this regard.

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar