简体   繁体   English

如何在导航到另一个页面后保留数组

[英]How to keep an array after navigation away to another page

Ok, so I have an array declared as so: 好的,所以我有一个声明如此的数组:

 var names_Array = [];

Then I am creating a list and that list is composed of the elements in the above array. 然后我创建一个列表,该列表由上面数组中的元素组成。 It is declared as so: 声明如下:

 var names_List = new WinJS.Binding.List(names_Array);

Based on a condition in my first page. 根据我的第一页中的条件。 I am adding items to this list, as so: 我正在向此列表中添加项目,如下所示:

 if (condition) {
     names_List.push({ name: "xxx", image: "image/xxx.png", ClientID: "xxxx" });
 }

Then I am moving to another page and my goal is for when I come back to have any of the elements which were added still there. 然后我移动到另一个页面,我的目标是当我回来时仍然有任何添加的元素。 I am aware, as of now that when I go back, the list is empty because of the line: 我知道,截至目前,当我回去时,由于该行,列表是空的:

var names_Array = [];

So my question is, is there any way to keep the items of the list, when I move away and come back. 所以我的问题是,当我离开并回来时,是否有任何方法可以保留列表中的项目。 I realise I need an additional if condition in the first page to determine if this is the first time on the page or if the user has gone away and come back. 我意识到我需要在第一页中有一个额外的if条件来确定这是第一次在页面上还是用户已离开并返回。 Something like: 就像是:

if (first){
    if(condition){
    sort out list;
    }
}
else{
    re-sort out list
}

Any ideas? 有任何想法吗?

There are a number of solutions you could try: 您可以尝试多种解决方案:

  1. store the information as a session object on the server and provide the data in the page each tie you visit. 将信息存储为服务器上的会话对象,并在您访问的每个绑定页面中提供数据。

  2. store the data in localStorage . 将数据存储在localStorage中

  3. store the data in a cookie . 将数据存储在cookie中

The right answer depends too much on details of your application, so I suggest researching each of this options and choosing the best fit. 正确的答案在很大程度上取决于您的应用程序的细节,因此我建议研究每个选项并选择最合适的选项。

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

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