Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use non-default localforage instance? #31

Open
siddo420 opened this issue Nov 9, 2015 · 5 comments
Open

how to use non-default localforage instance? #31

siddo420 opened this issue Nov 9, 2015 · 5 comments

Comments

@siddo420
Copy link

siddo420 commented Nov 9, 2015

how do I use separate localforage instances (instead of localforage)?

I am thinking of creating different instances per user (makes sense on installed apps).

Ref: https://github.com/mozilla/localForage#multiple-instances

@tofumatt
Copy link
Member

tofumatt commented Nov 9, 2015

I don't think localForage-backbone supports multiple instances yet. If you just want to change the db name per user you should be able to do that with the configuration.

Multiple instances are more useful when needing to deal with different sets of data at the same time. If you don't have multiple users signed in at once it might be overkill.

  • tofumatt (Sent from mobile)

On 9 Nov 2015, at 16:35, siddo420 [email protected] wrote:

how do I use separate localforage instances (instead of localforage)?

I am thinking of creating different instances per user (makes sense on installed apps).

Ref: https://github.com/mozilla/localForage#multiple-instances


Reply to this email directly or view it on GitHub.

@siddo420
Copy link
Author

siddo420 commented Nov 9, 2015

Makes sense, I will try that.

However, is it possible to switch stores through localforage.config or I need to close the existing/open store first? I don't know how to close and reopen a store once driver has been set.

@tofumatt
Copy link
Member

tofumatt commented Nov 9, 2015 via email

@siddo420
Copy link
Author

I tried something that seems to work without breaking existing code.

Here are the relevant, small changes I made (on top of many others).

    Backbone.LFFactory = function( instance ){
     return {
        localforageInstance: instance,
...

Then, I changed all locarforage instances to this.localforageInstance (inside save, update, find etc methods ).

Before returning (at the bottom):

...
    Backbone.localforage = Backbone.LFFactory( localforage );
    return Backbone.localforage;
..

So Backbone.localforage still uses the default localforage variable and for separate instances, I do:

var lf_instance = localforage.createInstance(...);
..
sync: Backbone.LFFactory( lf_instance ).sync('Accounts') // accounts collection

Can you think of any issues?

@stephanebachelier
Copy link
Contributor

@siddo420 I've not played with multiple instance of localforage instance but I've already managed to play with different stores. I mean I was able to switch between localstorage, websql and indexeddb depending on what the device supports. This was only made for a testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants