aboutsummaryrefslogtreecommitdiffstats
path: root/documentation
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>2017-01-25 12:27:44 +0200
committerDenis <denis@vaadin.com>2017-01-25 12:27:44 +0200
commit294ca0a2f5c29b65f9c4dc5887bfa0abc1cb7c7f (patch)
tree0a4630122dadc632e6508c85e4e79c717508de85 /documentation
parentbe694984fb35262b32c89be075e6d4a059931b62 (diff)
downloadvaadin-framework-294ca0a2f5c29b65f9c4dc5887bfa0abc1cb7c7f.tar.gz
vaadin-framework-294ca0a2f5c29b65f9c4dc5887bfa0abc1cb7c7f.zip
Add APIs to inform components of stale objects in DataProvider (#8271)
* Add DataProvider refreshItem for single item update * Add 'id' concept for DataProviders This patch also adds a simplified data provider that can replace items based on their id. This can be used to simulate stale objects from an actual backend. * Add refresh logic to Grid SelectionModels * Remove broken equals and hashCode * Refresh KeyMapper, clean up some methods * Fix UI.access in test * Fix tests and Grid single selection model * Do clean up before replacing data provider * Check correct variable for null value * Fix other selects, add generic tests * Code style fixes, removed assert * Merge remote-tracking branch 'origin/master' into 286_refresh_items * Fix documentation for refreshing an item * Improve introduction chapter, minor clarifications * Merge remote-tracking branch 'origin/master' into 287_refresh_items * Add missing parameters in unit tests
Diffstat (limited to 'documentation')
-rw-r--r--documentation/datamodel/datamodel-providers.asciidoc47
1 files changed, 46 insertions, 1 deletions
diff --git a/documentation/datamodel/datamodel-providers.asciidoc b/documentation/datamodel/datamodel-providers.asciidoc
index 6b5fea2275..80b9fb9cb4 100644
--- a/documentation/datamodel/datamodel-providers.asciidoc
+++ b/documentation/datamodel/datamodel-providers.asciidoc
@@ -181,7 +181,7 @@ Button modifyPersonButton = new Button("Modify person",
personToChange.setName("Changed person");
- dataProvider.refreshAll();
+ dataProvider.refreshItem(personToChange);
});
----
@@ -572,3 +572,48 @@ public class PersonDataProvider
}
}
----
+
+[[lazy-refresh]]
+=== Refreshing
+
+When your application makes changes to the data that is in your backend, you might need to make sure all parts of the application are aware of these changes.
+All data providers have the `refreshAll`and `refreshItem` methods.
+These methods can be used when data in the backend has been updated.
+
+For example Spring Data gives you new instances with every request, and making changes to the repository will make old instances of the same object "stale".
+In these cases you should inform any interested component by calling `dataProvider.refreshItem(newInstance)`.
+This can work out of the box, if your beans have equals and hashCode implementations that check if the objects represent the same data.
+Since that is not always the case, the user of a `CallbackDataProvider` can give it a `ValueProvider` that will provide a stable ID for the data objects.
+This is usually a method reference, eg. `Person::getId`.
+
+As an example, our service interface has an update method that returns a new instance of the item.
+Other functionality has been omitted to keep focus on the updating.
+
+[source, java]
+----
+public interface PersonService {
+ Person save(Person person);
+}
+----
+
+Part of the application code wants to update a persons name and save it to the backend.
+
+[source, java]
+----
+PersonService service;
+DataProvider<Person, String> allPersonsWithId = new CallbackDataProvider<>(
+ fetchCallback, sizeCallback, Person::getId);
+
+NativeSelect<Person> persons = new NativeSelect<>();
+persons.setDataProvider(allPersonsWithId);
+
+Button modifyPersonButton = new Button("Modify person",
+ clickEvent -> {
+ Person personToChange = persons.getValue();
+
+ personToChange.setName("Changed person");
+
+ Person newInstance = service.save(personToChange);
+ dataProvider.refreshItem(newInstance);
+});
+----