Skip to content
This repository has been archived by the owner on Aug 25, 2018. It is now read-only.

Provide customization of initial data sync #134

Closed
davideast opened this issue Jan 12, 2015 · 3 comments
Closed

Provide customization of initial data sync #134

davideast opened this issue Jan 12, 2015 · 3 comments

Comments

@davideast
Copy link
Contributor

In the current version of BackboneFire data is automatically synced when an autoSync model or collection is created.

Backbone.Firebase.Model.extend({
  url: 'https://<my-firebase>.firebaseio.com/user'
});

This can cause problems for authenticated or queried references. It would be nice to have a way to do some custom logic on a reference after it is created, but before the data is synced. This way a query can be placed on the reference or even authentication can happen. This would provide flexibility over setting custom options for authentication and querying.

Example API

Backbone.Firebase.Model.extend({
  url: 'https://<my-firebase>.firebaseio.com/user',
  preInit: function(ref) {
     // do authentication or something like that here
     // this would execute before the reference starts syncing data
     ref.authAnonymously();
  }
});

For those we do not with to sync immediately we could simply return false:

Backbone.Firebase.Model.extend({
  url: 'https://<my-firebase>.firebaseio.com/user',
  preInit: false
});

If you have thoughts on this, please let me know. I'm not a fan of the preInit name right now, but it is the best I can think of to illustrate the idea.

@carnevalle
Copy link

It would be awesome with a hook to handle different scenarios before sync, and perhaps a good name for the function could be onBeforeSync?

This would make it possible to authenticate, but what I like about the solution I proposed in #130 is that it reduces the amount of boilerplate code you have to write.

I also liked the fact that it communicated with the world through events, which would make it possible for me to create a parent view to handle authentication and errors in my views.

So I would opt for introducing this hook as well as also implementing authentication :-)

Perhaps something similar to this to accomodate more than just custom auth:

var authSettings = {
    provider: 'custom|password|anonymous',
    credentials: { // used with password provider
        email: "email",
        password: "password"
        remember: "default|sessionOnly|none"    
    },
    token: 'token' // used with custom provider
}

// Then you could add authSettings to the Model/Collection by extending
var Todo = Backbone.Firebase.Model.extend({
    auth: auth
});

// or pass them when you create a new Instance?
var model = new Todo({}, {
    auth: auth
});

And of course model/collections fire the appropriate events

collection.on('firebase:login:success', function(authData){
   // handle success
});
collection.on('firebase:login:error', function(error){
      // handle error
});

@davideast
Copy link
Contributor Author

This wouldn't be the total solution for authentication. It would be the platform to create customized models and collections that handle auth, like yours does.

I'd like to keep BackboneFire as flexible as possible. You can always extend a model or collection to do your custom logic.

@davideast davideast mentioned this issue Jan 13, 2015
9 tasks
@davideast
Copy link
Contributor Author

Closing in favor of #133

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

No branches or pull requests

2 participants