简体   繁体   中英

Persisting data client side for event driven approach

I have an application that renders activities and looks like this:

在此处输入图片说明

The map should render markers based on some criteria (date_start, date_end, map_bounds).
Activities are loaded from a REST api with ajax.
The front end approach is event driven (using Backbone / Marionette); activities are dynamically udpated when the date is changed, when the map is panned / zoomed, etc...

I wonder what is the best procedure to persist the parameters for the request in the front end (parameters being date_start, date_end, map bounds). Since all these data need to be accessible at any time, I need them to be globally accessible.
What is the common way to handle this?

I thought about using the localstorage with fallback to cookies, and storing the data in a window global.

When the app is opened in a new browser tab, there would be a bootstrap to retrieve the data and store them in a new window global.

Is there an alternate / better way to handle this?

If you're looking for data access from anywhere in your application, I would use a Backbone model that holds those parameters as properties; this model could be a global variable: having a single global variable would be better than individual ones for each piece of data. However, it would be better to pass that model as an argument to the views/other components that need it.

When the app is opened in a new browser tab, there would be a bootstrap to retrieve the data and store them in a new window global.

Because you're getting this data each time the application initially loads, localstorage or cookies aren't really the right thing to use (although technically possible). Localstorage and cookies are for persisting data across multiple user sessions. If you wanted to store the data in the browser (instead of retrieving the data from the server each time), you could do that and then load it into a Backbone model (as mentioned above). However, this wouldn't give you access to the data if the user was access your app from a second (nth) device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM