How to keep an array after navigation away to anot

2019-09-06 17:01发布

问题:

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. Something like:

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

Any ideas?

回答1:

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.

  3. store the data in a 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.