0

I have a number of drivers saved in objects:

function Driver(draw, name) {
    this.draw = draw;
    this.name = name;
}

Now, the number of drivers could change depending on how my form is used. It could be 5, it could be 60.

My understanding of using HTML5 Session storage is the syntax goes like this:

sessionStorage.setItem('name', value).

What I would like to do is have the ability to save them with the names driver1, driver2, driver, 3, etc. But, the numbers won't be consecutive and may include any amount up to 60.

Here's how I tried to do it, but it isn't working.

  $('.print').click(function () {
        for (var i = 0; i < 60; i++){ 
            var name3 = "."+"driver"+i;
            if (typeof Driver[name3] != "undefined") {
                console.log(name3);
                console.log(Driver[name3]);
                sessionStorage.setItem(Driver[name3], JSON.stringify(Driver[name3]));
            console.log(sessionStorage.getItem(Driver[name3]));
                entries[entries.length] = name3; 
            }
      console.log(entries);
      sessionStorage.setItem('entries', JSON.stringify(entries));

            };

What I tried to do is set a value for var name3, and then set the name of the session storage file to that value. It's pretty obvious to me I can't do that. I can't figure out another way of achieving my goal?

FYI, my attempt to sessionStorage 'Entries' is working perfectly.

Here's my entire project if you need to fiddle with it: http://precisioncomputerservices.com/slideways/index.html

  • How about putting the drivers in a normal array, then store the serialized version of the array with `sessionStorage.setItem('drivers',JSON.stringify(drivers))` and restoring it with `drivers = JSON.parse(sessionStorage.getItem('drivers'))`? – some Apr 12 '15 at 23:29

2 Answers2

0

It was posted as a comment but it's the answer.

Store everything in an array.

var allMyDrivers = [...];
sessionStorage["drivers"] = JSON.stringify(allMyDrivers);
var driversLater = JSON.parse(sessionStorage["drivers"]);
Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
  • I thought about using an array, but I'm not sure how to do that when each driver needs to keep track of the name and draw, like I set up here: `function Driver(draw, name) { this.draw = draw; this.name = name; }` – David Dubczak Apr 12 '15 at 23:33
0

Because your constructors arguments are retrievable from your Objects, you can just pass these through the constructor again in your retreival function

function storeDrivers(arr) {
    sessionStorage.setItem('drivers', JSON.stringify(arr));
}

function retrieveDrivers() {
    var arr = JSON.parse(sessionStorage.getItem('drivers')) || [];
    return arr.map(function (e) {return new Driver(e.draw, e.name);});
}

function forgetDrivers() {
    sessionStorage.removeItem('drivers');
}

storeDrivers([
    new Driver('foo', 'bar'),
    new Driver('fizz', 'buzz')
]);

retrieveDrivers();
// [
//     Driver {draw: "foo", name: "bar"},
//     Driver {draw: "fizz", name: "buzz"}
// ]

forgetDrivers();
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • Thanks. I used a slightly different method, but I was able to store my objects in an array and get them to retrieve properly on a different page. `Driver[name2] = new Driver(draw, name); DrivArray[DrivArray.length]=Driver[name2];}` and `sessionStorage.setItem('entrynames', JSON.stringify(DrivArray));` – David Dubczak Apr 14 '15 at 01:28