PDA

View Full Version : Is it incorrect behavior of JsonWriter with Direct?



stalek
17 Feb 2010, 4:36 AM
I try to send new record data from DirectStore using JsonWriter. In the body of the POST I don't see any ID items or something similar that can give me a chance to update a record on a server side and return a new ID. After short investigation I found probably source of the issue. JSON Writer has such method implementation to create new record:



createRecord : function(rec) {
return this.toHash(rec);
},


It means it always calls toHash() method. Body of toHash() method is below:



toHash : function(rec, config) {
var map = rec.fields.map,
data = {},
raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
m;
Ext.iterate(raw, function(prop, value){
if((m = map[prop])){
data[m.mapping ? m.mapping : m.name] = value;
}
});


This method checks if some rec.data element(property/attribute) has the same name like element on model (mapping) object and then it transfers that element to "data" object.
When in typical situation a record ("raw" variable) values are like below:



ElementID: ""
ExtSysID: ""
newRecord: true
newRecordId: "ext-gen334"
SortOrder: ""
Valid: ""
Value: "ppp"


it will never transfer newRecordId attribute to output data ("data" object in toHash() method) when the data model is like below:



ElementID: {...}
ExtSysID: {...}
SortOrder: {...}
Valid: {...}
Value: {...}



Does anybody have any method to implement CRUD (even just Create() method) using DirectStore ???

Maybe some custom writer resolves the problem here?

Alek

cerad
17 Feb 2010, 10:35 AM
I try to send new record data from DirectStore using JsonWriter. In the body of the POST I don't see any ID items or something similar that can give me a chance to update a record on a server side and return a new ID. After short investigation I found probably source of the issue. JSON Writer has such method implementation to create new record:
You are creating a new record so of course it does not have an id. It's the server's job to actually create the record in your database (or whatever), assign it a id and then send the id back to the client.

There are several examples on how to do this including a couple of work arounds. Search for them.

stalek
18 Feb 2010, 12:49 AM
Hi Cerad,
when you create new record it has some value managed by the store. It's newRecordId (for instance "ext-gen334"). This is the value that will be used by some handler to update your store with the data you send back from the server. When you send some records to the server (for instance 10 records) and change/update the value of one record only you should send this one record back to the client only(by me it's normal situation). Without this newRecordId it's very hard to detect what record has been modified by the server (or you have to send back the whole record set with all the records in the same order they have been send to the server) :(

So to be more precise (again): is it possible to send newrRecordIds to the server using JsonWriter (using some config options only)?

I'd like to avoid a situation when I have to create additional columns in record definitions just to deal with record creation on stores that should be resolved out of the box (or maybe I just don't know how to do it out of the box).

Ext.JS needs a list of Ids as an additional parameter when you send back a few modified records to the client only ...


Thanks,
Alek

cerad
18 Feb 2010, 8:41 AM
Without this newRecordId it's very hard to detect what record has been modified by the server (or you have to send back the whole record set with all the records in the same order they have been send to the server)
That is exactly what you need to do with the current implementation. Send back the exact same records (except for adding the id) in the exact same order. The document on Ext.Direct explains this.

The store is then automatically updated with the new information and basically starts fresh. There is no built in mechanism to determine what the server actually changed.

I don't disagree that the current implementation leaves something to be desired. Having to echo all the records back seems a bit wasteful and a bit fragile. But that is the way it currently is.

Judging from the notes in some bug reports it appears that the ExtJS team plans on doing a bit of over hauling. I'm sure they would welcome feature requests.

I'm not sure you really want to expose ext-genxxx to the server. Two clients could end up submitting two records with the same id. If you really wanted to assign the id's locally then write a bit of code to create a guid and go from there.