![](/style/images/good.png)
![](/style/images/bad.png)
Persist user setting and code optimization
source link: https://blogs.sap.com/2023/06/29/persist-user-setting-and-code-optimization/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Persist user setting and code optimization
In the previous blog, there are two ways to extend the user setting.
Normally, we need to persist the user option in the user setting. And the data should be based on user, it will record the user preference in the app.
The class in demoKit is useful: https://sapui5.hana.ondemand.com/sdk/#/api/sap.ushell.services.Personalization%23methods/Summary
The example code in previous blog post is enhanced to use to personalization service.
The brief step are:
- get personalization service.
- get container.
- use container method.
The difficulty is the service/container is returned as parameter in asynchronous.
var oRenderer = sap.ushell.Container.getRenderer("fiori2");
...
var oContainerService = sap.ushell.Container.getServiceAsync("Personalization");
//Call the getServiceAsync to get the Unified Shell's personalization service
oContainerService.then(function (oService) {
//The personalization service will be returned as parameter
oService.getContainer("ImportOption").done(function (oPContainer) {
//Use the personalization service to get container
//Check the method detail https://sapui5.hana.ondemand.com/sdk/#/api/sap.ushell.services.Personalization%23methods/getContainer
//The container also will be returned as parameter
if (!oPContainer.containsItem("Option1")) {
oPContainer.setItemValue("Option1", false);
oPContainer.save();
}
var handlePress = function (e) {
var oDialog = new sap.m.Dialog({
content: [new sap.m.CheckBox({
text: "Option",
selected: oPContainer.getItemValue("Option1"),
select: function() {
oPContainer.setItemValue("Option1", !oPContainer.getItemValue("Option1"));
}
})],
endButton: new sap.m.Button({
text: "Confirm",
press: function (e) {
//Call save method to persist data
oPContainer.save();
e.getSource().getParent().close();
}.bind(this)
})
});
oDialog.open();
}
var button1 = new sap.m.Button({ text: "testmybutton", press: handlePress });
oRenderer.showActionButton([button1.getId()], false, ["home", "app"]);
Code optimization:
In the previous code, there are two asynchronous calls to get container. And the operation code like showActionButton need to be called in the then method. Since the method needs to wait for the result in personalization service.
We can separate the “Promise” objects and use $.when() to avoid the nested call.
The optimized code:
var oServicePromise = sap.ushell.Container.getServiceAsync("Personalization").then(function(oService){ return oService});
var oContainerPromise = oServicePromise.then(function(oService){ return oService.getContainer("ImportOption")});
$.when(oContainerPromise).then(function(oContainer){
if (!oContainer.containsItem("Option1")) {
oContainer.setItemValue("Option1", false);
oContainer.save();
}
var handlePress = function (e) {
var oDialog = new sap.m.Dialog({
content: [new sap.m.CheckBox({
text: "Option",
selected: oContainer.getItemValue("Option1"),
select: function() {
oContainer.setItemValue("Option1", !oContainer.getItemValue("Option1"));
}
})],
endButton: new sap.m.Button({
text: "Confirm",
press: function (e) {
//Call save method to persist data
oPContainer.save();
e.getSource().getParent().close();
}.bind(this)
})
});
oDialog.open();
}
var button1 = new sap.m.Button({ text: "testmybutton", press: handlePress });
oRenderer.showActionButton([button1.getId()], false, ["home", "app"]);
});
The key is about the principle “Return Value: This method can either return a Promise (if further another then() is called) or nothing.” for then() method:
Recommend
-
37
README.md
-
22
(Last Updated On: February 27, 2019)
-
34
README.md vim-qlist Vim-qlist is an updated, more powerful, version of the function discussed in
-
51
To set correct system variable values is the essential step to get the correct server behavior against the workload. In MySQL, we have...
-
34
README.md Redux Persist Persist and rehydrate a redux store.
-
34
Fleeceware apps persist on the Play Store Fleeceware remains a problem on Google Play, where Android users still run the risk of being charged hundreds of dollars or euros for “subscriptions” to apps...
-
7
Reading Time: 4 minutes In this blog, we will look at various ways in which storage from the host machine can be mounted to containers. Also, it can be seen as a way of communication in case the networking is disabled for your containers. So...
-
5
Persist existing data when setting up a Rails development environment By Aaron Sumner, February 28, 2021. File under:
-
3
How to Persist Web App Data in the User File SystemWe are going to build a text editor to save and open text files from the file systemMost web apps need to persist user data to deliver value. It could be anything from...
-
13
Extracting stock Boot, Recovery and Persist images ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK