Skip to Content
Menu
This question has been flagged
1 Reply
1961 Views

I created a "ir.actions.client" component just like: (just simple example, pls ignore the coding errors, just help to explain my question)


class TestComponent extends Component {
static template = xml`
    
    
        
    
`;
   setup() {...}
   this.params_test = useState("");
   this.results = useState([]); 
   dosomething(){  ...  }
}
TestComponent.template = "test_template";
registry.category("actions").add("testcomponent", TestComponent);


from this page, I submit parameter and get the values from backend, and render the "results" to the page. then I select on record of results and click it to its detailed page(actually it's in another module), after that when I click back to the "TestComponent" page, all results disappeared, also without the parameter i submitted --- it's just a initial page.

My question, how to store the "TestComponent" page state when go to another page, and can find them when back to this page?



Avatar
Discard
Best Answer

Hi,

To address this issue, you can store your data in the cache using the following steps:

Step 1:
You can utilize the onWillDestroy hook to save the current page's values when you navigate to another page.

Step 2:
Inside the onWillDestroy hook, set the cache values.

Step 3:
When returning to the previous page, retrieve the values from the cache.

Here's an example of how to implement this:
import { onMounted, Component, onWillDestroy } from "@odoo/owl";

class TestComponent extends Component {
    setup() {
        onWillDestroy(async () => {
            // Prepare values for caching
            const values = {
                time: this.time,
                content: 'Demo Content'
            };
           
            // Store values in local storage for potential resumption
            await browser.localStorage.setItem('AnalyticCacheResume', JSON.stringify(values));
        });

        onMounted(async () => {
            // Retrieve cached values from local storage for initial timer setup
            const values = JSON.parse(browser.localStorage.getItem('AnalyticCache') || '{}');
           
            // Log the retrieved values to the console
            console.log(values);

            // If you need to remove the cached data, you can use:
            browser.localStorage.removeItem('AnalyticCacheResume');
        });
    }
}

Note:-Change the owl hooks according to your use case.Please refer the below link to know more about owl hooks

Owl Hooks

Avatar
Discard
Related Posts Replies Views Activity
2
Jul 24
4530
0
Jan 24
1277
0
Sep 23
1409
1
Jan 25
3313
1
Jun 24
3755