PDA

View Full Version : [CLOSED] Store GetChangedData and Phantom record confusion



jchau
Mar 20, 2013, 12:58 PM
When I add a new record to store, I want to generate the ID property on the clientside. But when I set the ID property on the record, it marks the record as not phantom...so it's not new. What's the logic behind this? It seems silly and counterintuitive that I can't generate IDs on the clientside.

Vladimir
Mar 20, 2013, 1:09 PM
Any record which has id automatically becomes to non phantom.

Text from phantom property documentation


True when the record does not yet exist in a server-side database (see setDirty (http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Model-method-setDirty)). Any record which has a real database pk set as its id property is NOT a phantom -- it's real.

Such logic is implemented in ExtJS Model class. You can try to set phantom property to true manually after adding to a store (please note that by default id value of phatom records is not passed to the server during sync action)

Can you explain a reason why do you need to set id on the client side?

jchau
Mar 20, 2013, 1:15 PM
Because there are alot of javascript code that references records by id. So when the record doesn't have a generated ID, all my functions break for new records. I need the ID to be a format of a GUID string. I even tried manually setting phantom = true but in your custom getChangedData function, it strips out the id property from the return data to the server.

Vladimir
Mar 20, 2013, 1:26 PM
Because there are alot of javascript code that references records by id.

Why don't you want to save records first before using that code? Also you can use internalId property of the record and getByInternalId method of store to find a record



I even tried manually setting phantom = true but in your custom getChangedData function, it strips out the id property from the return data to the server.

Yes, as I mentioned before, id is not passed for phantom records to the server (because, by default, store expects real id from the server for phantom records). You can change that behaviour using SkipIdForPhantomRecords propety of Writer

jchau
Mar 20, 2013, 2:14 PM
skipID property worked for me. Thanks!

But for your other question, here's a scenario of why IDs should be generated on the clientside:

1 grid with Order and 1 grid with OrderLineItems. When adding a new order, I need to set the OrderGUID on that record so when creating new order line items for that new order, I can set the OrderLineItem.OrderGUID. I obviously do not want to save the new order when the add a new Order record because they may to want to add a bunch of orders and a bunch of order line items before saving.

IMO, this is ExtJS guys being lazy and trying to pigeonhole how we suppose to manage records. They are assuming that new records will get saved to a database and it's the database's job to generate new IDs. Maybe they are assuming only autoincrement IDs should be used? Being a clientside framework, they shouldn't force us into certain serverside patterns.

Daniil
Mar 21, 2013, 7:12 AM
It is definitely good to ask on the Sencha forum.