Firebase bindings for batman.js
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
doc
examples
spec
src
.gitignore
batfire.coffee
batfire.js
batfire.min.js
bower.json
package.json
readme.md

readme.md

BatFire

BatFire is a Firebase client library for batman.js. It's available in CoffeeScript, JavaScript, and minified JavaScript. BatFire offers:

Also see example security rules, the Jasmine spec suite or an example app.

Usage

  • Get the files:

    Install with Bower:

    bower install batfire
    

    Or, download BatFire in a form of your choice: CoffeeScript, JavaScript, Minified JavaScript

  • Load the files:

    Be sure to include BatFire after you include Firebase and batman.js. For example:

    <script src='/path/to/firebase.js'></script>
    <script src='/path/to/batman.js'></script>
    <script src='/path/to/batfire.js'></script>
  • @syncsWithFirebase("your-app")

    For example,

    class MyApp extends Batman.App
      @syncsWithFirebase "my-firebase-app-name"

    will sync with https://my-firebase-app-name.firebaseio.com.

BatFire.Storage

BatFire.Storage implements the Batman.StorageAdapter interface, so you can pass it to @persist in your model definition. (Learn more about BatFire.Storage)

For example:

class App.Sandwich extends Batman.Model
  @resourceName: 'sandwich'
  @persist BatFire.Storage
  @encode "meats", "tomato", "lettuce"
  @encodesTimestamps()

Now, all the storage operations of your records will trigger updates on Firebase:

blt = new App.Sandwich(meats: ["bacon"], lettuce: true, tomato: true)
blt.save()    # => BLT will appear for all clients!
blt.get('id') # => "-JELsmNtuZ4FX6D5f_Ou" and the like
blt.destroy() # => BLT will be gone from all clients!

App.Sandwich.find "-JELsxaWRqaDlDZJHw3y", (err, record) ->
  record.get('name') # => 'Reuben'

Items added, removed, and changed on Firebase will be propagated to all connected loaded sets.

App.Sandwich.get('all') # => starts listening for new sandwiches on Firebase, adds them to `Sandwich.loaded`
App.Sandwich.clear() # => clears the loaded set, stops listening for new sandwiches

Authorization

BatFire provides a lightweight wrapper around FirebaseSimpleLogin. (Learn more about authorization)

You define providers in your app definition:

class App extends Batman.App
  @syncsWithFirebase('my-app-name')
  @authorizesWithFirebase('github', 'facebook')

There are also view helpers:

<div data-showif='loggedIn'>
  <span data-bind='currentUser.username | prepend "Welcome, " | append "!"'></span>
  <button data-event-click='logout'>Log out</button>
</div>
<div data-showif='loggedOut'>
  <button data-event-click='login | withArguments "github"'>Log in</button>
</div>

Model.belongsToCurrentUser

If your app syncsWithFirebase and authorizesWithFirebase, you can get some out-of-the-box features on your models, too. (Learn more about belongsToCurrentUser)

Call this inside a model definition:

class App.Sandwich extends Batman.Model
  @belongsToCurrentUser(scoped: true, ownership: true)
yourSandwich.get('hasOwner')              # => true
yourSandwich.get('isOwnedByCurrentUser')  # => false
yourSandwich.get('created_by_uid')        # => "github:123456"

App.syncs

App.syncs binds keypaths on your app to Firebase so that if they're updated on client, the updates are pushed to the others:

class App extends Batman.App
         # key...            # optional: constructor name for loading from Firebase, relative to App
  @syncs 'sandwichOfTheDay', as: "Sandwich"

(Learn more about App.syncs)

To do

  • Allow custom function for generating IDs
  • shorthand App.syncsWithFirebase(key, {authorizes: authArguments})

Development

  • npm install
  • tests: npm run-script spec
  • build: npm run-script build

License: MIT