1.0.13 • Published 4 years ago

instancebox v1.0.13

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

instanceBox

What: You want to store javascript instances in the browser
Why: I dont care
How: this module aims to offer a solution

Important

This is a draft module, does not work when the instance references other objects within. Looks like it is not easy to have that but I'm working on that.

build and run test


what to expect

In page one.html:

// a constructor is available
function Person(name, age){
    this.name = name;
    this.age = age;
    
    // not yet
    // this.doc = new Doc(this.name. this.age)
}
Person.prototype.sayHello = function () {
    return 'Hi, my name is ' + this.name;
};

// we create a instance
var me  = new Person('Federico', 42);

// now we would like to save it (local storage is the default one)
instanceBox.set('thisIsMe', me);

That's it, now we are free to navigate to another page and fully retrieve the instance. Let`s go for instance at page two.html.
We`ll be able to retrieve the instance simply using the get method passing the right key:

var me = instanceBox.get('thisIsMe');
console.log(me.sayHello()) // ---> Hi, my name is Federico

What about sessionStorage ?

To chance storage is enough to call the two functions:

  • instanceBox.useSessionStorage()
  • instanceBox.useLocalStorage()

before saving / retrieving from the target storage.


something more

instanceBox offers the following methods:

  • useLocalStorage()
    sets localStorage as container
    returns: void

  • useSessionStorage()
    sets sessionStorage as container
    returns: void

  • set(key <string>, instance <object instance>)
    saves the given instance using the key provided
    returns: true OR Exception

  • get(key <string>)
    attempts to retrieve the instance that has been saved with the given key
    returns: Object OR null

  • remove(key <string>)
    if found removes the instance saved using the passed key
    returns: void

  • clear()
    completely cleans out the container
    returns: void

  • length()
    returns the number of elements saved
    returns: Number

  • getSize(key <string>)
    returns the size in bytes of the instance saved using the given key
    returns: Number OR null

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago