Search code examples
javascriptnode.jsbackbone.js

Retaining a value from a response in node js


On page load, say for a particular route for (e.g. https://localhost:8000/home/index), a service is called and the response from the service is rendered to the page at the client side.

On the same page, I have a link that pops up a Backbone.js modal and from the modal a click event triggers which hits another url (e.g. https://localhost:8000/home/index2) upon which another service call triggers and the response is rendered to another html page.

On the same html page, I want to display a value which I got from the first service call on page load. However, I am unable to retain that value as there are two different requests each time. Hence, I cannot even append the value from first response to the request object and use it a second time.


Solution

  • You can use JavaScripts Web Storage API to storage information on client browser.

    MDN Web Storage API

    For example, If you are on the first screen and call a service, store the service information on localStorage

    localStorage.setItem('firstService', serviceResponseObject);

    Once you are navigated to second page, you can use localStorage to read to previous service information

    localStorage.getItem('firstService');