简体   繁体   English

坚持数据客户端的事件驱动方法

[英]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). 地图应根据某些条件(date_start,date_end,map_bounds)渲染标记。
Activities are loaded from a REST api with ajax. 活动是使用ajax从REST api加载的。
The front end approach is event driven (using Backbone / Marionette); 前端方法是事件驱动的(使用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). 我想知道什么是将请求的参数持久保存在前端的最佳方法(参数为date_start,date_end,地图范围)。 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. 我考虑过将本地存储与cookie一起使用,并将数据存储在全局窗口中。

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; 如果您要从应用程序中的任何位置寻找数据访问,我将使用将这些参数作为属性保存的Backbone模型; 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). 因为每次应用程序初始加载时都会获取此数据,所以使用本地存储或cookie并不是真正正确的选择(尽管在技术上是可行的)。 Localstorage and cookies are for persisting data across multiple user sessions. Localstorage和Cookie用于在多个用户会话之间持久存储数据。 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). 如果要将数据存储在浏览器中(而不是每次都从服务器检索数据),则可以这样做,然后将其加载到Backbone模型中(如上所述)。 However, this wouldn't give you access to the data if the user was access your app from a second (nth) device. 但是,如果用户正在从第二(第n)台设备访问您的应用程序,则不会给您访问数据的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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