-->

Table Not Updating With Manual Save Mode

2019-07-23 04:07发布

问题:

So I have a table that shows the entries. Users click on a button to open a fragment page to edit the data.

app.datasources.SystemOrders.selectKey(widget.datasource.item._key);
app.showDialog(app.pageFragments.SystemOrders_Edit);

This part works fine.

I have changed my datasource to Manual Save Mode to be able to utilize the "email notification for changes" functions that are used in the Project Tracker sample. So that a user can make changes, hit a Save (Close) Button and an email goes out showing the changes.

The problem is that when the user closes the fragment, the table does not update (they have the same datasource). When I was in automatic save mode, I was able to utilize the following to force the table to reload so it reflected any changes:

var datasource = app.datasources.SystemOrders_HideComplete;
datasource.load();
app.closeDialog();

So I figured I just needed to add the widget.datasource.saveChanges(); option for the Close Button.

widget.datasource.saveChanges();
var datasource = app.datasources.SystemOrders_HideComplete;
    datasource.load();
    app.closeDialog();

Unfortunately, when I use the above I get the following error and the table seems like it gets stuck trying to reload (the spinner keeps spinning).

"Cannot query records on models with changes."

I'm assuming this is maybe because the datasource hasn't finished saving the new change, before trying to reload the datasouce?

How can I have the Save (Close) Button:

Save the changes

Close the dialog

Refresh the table so it reflects the changes?

Thank you for your help.

回答1:

You can try to refresh datasource in save callback(assuming that you are actually sharing datasource between page fragment and page):

widget.datasource.saveChanges(function() {
  widget.datasource.load();
  app.closeDialog();
});


回答2:

That error will happen if your datasource is trying to refresh while you have unsaved changes.

I know your code block calls a client-side saveChanges before loading the datasource again, but you may need to check to make sure that the datasource is not being reloaded elsewhere at the same time.

If that hasn't fixed it, try passing the values to a server-side script and save the changes after assigning the properties, like this:

Client:

function handleSuccess() {
  // ensures that records are saved, now reload
  app.models.YourModel.YourDatasource.load();
}

google.script.run.withSuccessHandler(handleSuccess).addItem(field_value1, field_value2, field_value3); 

Server:

function addItem(field_value1, field_value2, field_value3) {

 // create new items
 var newItem = app.models.YourModel.newRecord();

 // assign values
 newItem.field1 = field_value1; 
 newItem.field2 = field_value2; 
 newItem.field3 = field_value3; 

 // save
 app.saveRecords([newItem]);
}

I've found this method to be more reliable than manipulating changes from the client script.