PDA

View Full Version : When is autogenerated id from server set on a newly created record?



erikras
31 Aug 2011, 11:00 AM
I'm creating a record and adding it to my store. But the record value returned from Store.add() and the record passed to the store's "add" listener still don't have their id set, and are still marked as phantom. The "add" listener must not be waiting for the result from the server to be parsed.

I need the id of the record I just created immediately after I create it to perform another operation. Is there a listener for when the record gets updated with the autogenerated values from the database and phantom gets set to false?

Please don't tell me I have to listen to all ajax calls and figure out which one is being sent from the store!

erikras
31 Aug 2011, 12:37 PM
I've tracked it down to Store.onCreateRecords(), which is a private function, and there are no events associated with it. It removes the original phantom record and then inserts the newly loaded record, but it references the data element inside the store directly.

So.... one solution is to put an "add" listener onto the store.data MixedCollection. This will fire once when you add the phantom element, and then again when the non-phantom element is added by Store.onCreateRecords(). This feels horribly messy.

Store really should have a "record added and here's the non-phantom instance of it" event for this purpose.

erikras
31 Aug 2011, 2:17 PM
Okay, so it appears that the proper way might be:


store.on('write', function(store, operation) {
if(operation.action == 'create') {
var record = operation.resultSet.records[0];
// do whatever you want with your newly returned canonical (phantom=false) record
}
});

It's not pretty, but it works. Ideally, I'd prefer a "create" even on Store that returns the created records, rather than having to dig into the resultSet of the operation.