cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI 5 Auto refresh

12-06-2021 10:51 AM
4590 views 11 comments Go to solution
0 Likes
SAP Managed Tags
Subscribe

Hi all,

I want to auto refresh my UI5 Project. Here is my code from an other question in a controller. sap.ui.define([

"sap/ui/core/mvc/Controller"

], function (Controller) {

"use strict";

return Controller.extend("DurchstichSYS.DurchstichSYS.controller.refresh", {

[window.]setInterval(functionToRefreshModel,1000); //Trigger this function every 1000 milliseconds

}

});

});

I get a error like "parsing error unexpected token". Please help me.

0 Likes

Accepted Solutions (1)

Accepted Solutions (1)

0 Likes
sap.ui.define([
			"sap/ui/core/mvc/Controller"
			
		], function (Controller) {
			"use strict";

			return Controller.extend("DurchstichSYS.DurchstichSYS.controller.Durchstich", {

					onBeforeRendering: function () {

						setInterval(function () {

                             this.location.reload(true);
							// sap.ui.getCore().byId("Durchstich").getModel("").refresh(true);

							}, 10000);

}
					});
			});

Answers (3)

Answers (3)

junwu
SAP Champion
SAP Champion

you'd better learn some basic javascript, what you are asking has nothing to do with ui5.

mariusobert
Developer Advocate
Developer Advocate
0 Likes

I agree with Jun. I don't mean this in a rude way (and I'm sure Jun doesn't either). But I don't think you'll be able to solve that problem by asking these questions. I recommend doing a JS training on Pluralsight, Lynda.com or any of the other learning platforms.

mariusobert
Developer Advocate
Developer Advocate

Yes, this message is correct as you cannot enter arbitrary JavaScript code within an object. Have a look here to see what the expected syntax of a UI5 controller is. You probably want to call setInterval in the onInit method of the controller.

sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function (Controller) {
    "use strict";
    return Controller.extend("sap.ui.demo.walkthrough.controller.App", {
        onInit: function () {
            setInterval(functionToRefreshModel, 1000); //Trigger this function every 1000 milliseconds
        }
    });
});



I also should mention that using setInterval to refresh a model smells a bit like an anti pattern.

PS: Please make use of the code formatter next time you ask a question. This makes it easier to read and answer your question 🙂

0 Likes

Ok, thanks for your advice, but do you know of a better way to display live data in the view that automatically updates? Could you show me please.

Jetzt bekomme ich die Fehlermeldung, dass functionToRefreshModel nicht definiert ist, das ist wahr, wie kann ich das tun?


mariusobert
Developer Advocate
Developer Advocate
0 Likes

Let's stay in English here so that non-German speakers can follow the discussion as well 🙂

This approach is ok if you do it carefully. But you need to make sure the interval is only initialized once and to clear it when the controller is destroyed.

And yes, you need to implement that function as well. It's up to you where you define it but I needs to be in scope of the onInit function. This is a pure JS question and not related to the UI5 framework.

0 Likes

Thank you for the answer! I get again the error with the unexcpected token.

sap.ui.define([
	"sap/ui/core/mvc/Controller"
], function (Controller) {
	"use strict";

	return Controller.extend("DurchstichSYS.DurchstichSYS.controller.Durchstich", {
		onInit: function () {
			
            setInterval( functionToRefreshModel , 1000 ); // Diese Funktion alle 1000 Millisekunden auslösen
          
          functionToRefreshModel{
			DurchstichSYS.DurchstichSYS.view.Durchstich.reload();
		}
		}
		
          }); 

});	
0 Likes

mariusobert

junwu
SAP Champion
SAP Champion
0 Likes
function functionToRefreshModel(){
			DurchstichSYS.DurchstichSYS.view.Durchstich.reload();
		}
0 Likes

I still get a Parsing error: unexpected token fuction [ESLINT: () ]

jun.wu5

sap.ui.define([
	"sap/ui/core/mvc/Controller"
], function (Controller) {
	"use strict";

	return Controller.extend("DurchstichSYS.DurchstichSYS.controller.Durchstich", {
		onInit: function () { 
			// falsches event
			[window].setInterval(functionToRefreshModel , 1000 ); // Diese Funktion alle 1000 Millisekunden auslösen
		}
          function functionToRefreshModel(){
			DurchstichSYS.DurchstichSYS.view.Durchstich.reload();
		}
		
		}
		
          }); 

});
junwu
SAP Champion
SAP Champion
0 Likes
sap.ui.define([
	"sap/ui/core/mvc/Controller"
], function (Controller) {
	"use strict";

	return Controller.extend("DurchstichSYS.DurchstichSYS.controller.Durchstich", {
		onInit: function () { 
function functionToRefreshModel(){ DurchstichSYS.DurchstichSYS.view.Durchstich.reload(); } // falsches event [window].setInterval(functionToRefreshModel , 1000 ); // Diese Funktion alle 1000 Millisekunden auslösen } } }); });