I save the data from the form to the local storage, and then send it to the server. If the connection is lost, the data must be written to another storage, so that, when the connection is restored, it must be sent and transferred to the first storage (successful). This option does not work, because both storages refer to the same object.

SucessStorage = localStorage; UnsucessStorage = localStorage; 

    2 answers 2

    I think if you need to branch the repository, why not use it. in the name of the variable, for example:

     localStorage.setItem("success.some", 1); localStorage.setItem("unsuccess.some", 2); console.log(localStorage['success.some']); console.log(localStorage['unsuccess.some']); 

    Otherwise there is no branching. There is truth still sessionStorage but would not advise crutches and write them strictly on the category of data.

    • I came to this option myself. If there is no other solution, then I will certainly use it. But he has plenty of minuses. 1) When I send data after the connection is resumed, it will be necessary to find data in the repository with a certain sequence in the key (flag, "unsuccess" for example). Then, after successfully sending, change the flag to "success", while maintaining the numbering! Quite not easy and resource intensive. 2) Local storage will eventually fill up, there may be 500 and 2000 records, and all of them will have to go through every time !! - Pavel Saenko
    • If it strains, there is for example a solution to expand the Storage prototype ( localStorage instanceof Storage = true ). Napimer add methods setBranch, getBranch, and override other methods. Or write your own wrapper. Storage is a means, in the end it is the norm to write your own wrapping class. - Goncharov Alexander
    • And can this site be more detailed? Without an example I can not fully understand. - Pavel Saenko 1:54 pm
    • @PavelSaenko The key prefix option is the easiest. The solution can be simplified if you refuse to store data from the form in case of successful submission. If the data is successfully sent - why keep it in storage? Only one option comes to mind - to auto-fill this form. But in this case, it probably makes sense to store not 2 sets of data, but one that the user entered last + service 1 flag successfully sent the data or not. - M.Onyshchuk
    • @ M.Onyshchuk Totally agree with you. I also do not understand why to store the sent data. But not for me to decide. The task was formulated by the head of industrial practice. I immediately told her that it would be much easier to refuse to store the sent data. Probably should talk to her again. (A few words about the task itself: ASP MVC web application, for the port tablet terminal: cargo data (number, customer, etc.) gets clogged up in the form and sent to the server. If the connection is lost, the data is saved locally and every 30 seconds they try again pass) - Pavel Saenko 2:51 pm

    By the way. There is a very interesting feature in DevExtreme.

     var store = new DevExpress.data.LocalStore({ name: "MyLocalData", key: "id", customfield: "Ваше поле" }); 

    thus, an entry is created in the local storage with the key dx-data-LocalStore - name, in the value of which the necessary fields are stored. The number of fields is not limited. Further, using the available methods of recording, updating, sorting, etc. You can use this record as a full repository. And such storages can be arbitrarily many. It turns out LocalStorage inside LocalStorage.

    • in fact, this is the realization of an idea from a nearby response, just a wrapper over localStorage - Grundy 6:42 pm