2

Is record.set synchronous or asynchronous? If I want to be sure that code I put after record.set is executed when document is completely updated should I wrap my code with record.whenReady(function() {...})?

Consider the code looks like:

 var record = ds.record.getRecord(`table/${id}`);
 record.whenReady(function () {
   record.set('field', 'value');
   // do I need whenReady here?
   // code that should be executed when document is updated
 })

1 Answers1

2

record.set is sync since as soon as you call it will store the value and trigger the related local listeners you have within your application.

var record = ds.record.getRecord(`table/${id}`);

record.subscribe( 'field', function( data ) {
    //do something
} );

record.whenReady(function () {
        record.set('field', 'value'); // this will trigger the subscribe callback
        record.get( 'field' )  // now returns 'value'
})

whenReady should be used to guarantee the current data for the record has been loaded before trying to read/write to it. If it's the first time the record has been requested it will wait for the server to respond with the records content, otherwise it will use the cached record state stored within the client.

In summary, after calling .set you can be assured your document has been updated!

yasserf
  • 391
  • 1
  • 7