From 100ce00a11c5aca811315c47eac857e5d6d9c052 Mon Sep 17 00:00:00 2001 From: Matt Marcha Date: Fri, 20 Sep 2024 14:34:34 -1000 Subject: [PATCH] ch2.5 Cache network calls, create a service --- awesome_dashboard/static/src/dashboard.js | 4 ++-- .../static/src/statistics_service.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 awesome_dashboard/static/src/statistics_service.js diff --git a/awesome_dashboard/static/src/dashboard.js b/awesome_dashboard/static/src/dashboard.js index c178ee2..5d03968 100644 --- a/awesome_dashboard/static/src/dashboard.js +++ b/awesome_dashboard/static/src/dashboard.js @@ -17,9 +17,9 @@ class AwesomeDashboard extends Component { this.action = useService("action"); - this.rpc = useService('rpc'); + this.stats = useService('awesome_dashboard.statistics'); onWillStart(async () => { - this.stats = await this.rpc("/awesome_dashboard/statistics"); + this.stats = await this.stats.loadStatistics(); }); } diff --git a/awesome_dashboard/static/src/statistics_service.js b/awesome_dashboard/static/src/statistics_service.js new file mode 100644 index 0000000..1ee547b --- /dev/null +++ b/awesome_dashboard/static/src/statistics_service.js @@ -0,0 +1,17 @@ +/** @odoo-module **/ + +import { registry } from "@web/core/registry"; +import { memoize } from "@web/core/utils/functions" + +const statisticsService = { + dependencies: ["rpc"], + async: ["loadStatistics"], + start(env, { rpc }) { + return { + loadStatistics: memoize(() => rpc("/awesome_dashboard/statistics")), + }; + }, +}; + + +registry.category("services").add("awesome_dashboard.statistics", statisticsService); \ No newline at end of file