• This repository has been archived on 25/Aug/2018
  • Stars
    star
    291
  • Rank 141,430 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Backbone bindings for Firebase

Status: Archived

This repository has been archived and is no longer maintained.

status: inactive

BackboneFire

Build Status Coverage Status Version

BackboneFire is the officially supported Backbone binding for Firebase data. The bindings let you use special model and collection types that allow for synchronizing data with Firebase.

Live Demo

Play around with our realtime Todo App demo. This Todo App is a simple port of the TodoMVC app using BackboneFire.

Basic Usage

Using BackboneFire collections and models is very similar to the regular ones in Backbone. To setup with BackboneFire use Backbone.Firebase rather than just Backbone.

Note: A Backbone.Firebase.Model should not be used with a Backbone.Firebase.Collection. Use a regular Backbone.Model with a Backbone.Firebase.Collection.

// This is a plain old Backbone Model
var Todo = Backbone.Model.extend({
  defaults: {
    completed: false,
    title: 'New todo'
  }
});

// This is a Firebase Collection that syncs data from this url
var Todos = Backbone.Firebase.Collection.extend({
  url: 'https://<your-firebase>.firebaseio.com/todos',
  model: Todo
});

Downloading BackboneFire

To get started include Firebase and BackboneFire after the usual Backbone dependencies (jQuery, Underscore, and Backbone).

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- Underscore -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore.js"></script>

<!-- Backbone -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone.js"></script>

<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.0.3/firebase.js"></script>

<!-- BackboneFire -->
<script src="https://cdn.firebase.com/libs/backbonefire/0.5.1/backbonefire.js"></script>

Use the URL above to download both the minified and non-minified versions of BackboneFire from the Firebase CDN. You can also download them from the releases page of this GitHub repository. Firebase and Backbone can be downloaded directly from their respective websites.

You can also install BackboneFire via Bower and its dependencies will be downloaded automatically:

$ bower install backbonefire --save

Once you've included BackboneFire and its dependencies into your project, you will have access to the Backbone.Firebase.Collection, and Backbone.Firebase.Model objects.

Getting Started with Firebase

BackboneFire requires the Firebase database in order to sync data. You can sign up here for a free account.

autoSync

As of the 0.5 release there are two ways to sync Models and Collections. By specifying the property autoSync to either true of false, you can control whether the component is synced in realtime. The autoSync property is true by default.

autoSync: true

var RealtimeList = Backbone.Firebase.Collection.extend({
  url: 'https://<your-firebase>.firebaseio.com/todos',
  autoSync: true // this is true by default
})
// this collection will immediately begin syncing data
// no call to fetch is required, and any calls to fetch will be ignored
var realtimeList = new RealtimeList();

realtimeList.on('sync', function(collection) {
  console.log('collection is loaded', collection);
});

autoSync: false

// This collection will remain empty until fetch is called
var OnetimeList = Backbone.Firebase.Collection.extend({
  url: 'https://<your-firebase>.firebaseio.com/todos',
  autoSync: false
})
var onetimeList = new OnetimeList();

onetimeList.on('sync', function(collection) {
  console.log('collection is loaded', collection);
});

onetimeList.fetch();

Backbone.Firebase.Collection

This is a special collection object that will automatically synchronize its contents with your Firebase database. You may extend this object, and must provide a Firebase database URL or a Firebase database reference as the url property.

Each model in the collection will have its own firebase property that is its reference in Firebase.

For a simple example of using Backbone.Firebase.Collection see todos.js.

var TodoList = Backbone.Firebase.Collection.extend({
  model: Todo,
  url: 'https://<your-firebase>.firebaseio.com/todos'
});

You may also apply an orderByChild or some other query on a reference and pass it in:

Queries

var TodoList = Backbone.Firebase.Collection.extend({
  url: new Firebase('https://<your-firebase>.firebaseio.com/todos').orderByChild('importance')
});

url as a function

The url property can be set with a function. This function must return a Firebase database ref or a url.

var TodoList = Backbone.Firebase.Collection.extend({
  url: function() {
    return new Firebase(...);
  }
});

initialize function

Any models added to the collection will be synchronized to the provided Firebase database. Any other clients using the Backbone binding will also receive add, remove and changed events on the collection as appropriate.

You should add and remove your models to the collection as you normally would, (via add() and remove()) and remote data will be instantly updated. Subsequently, the same events will fire on all your other clients immediately.

add(model)

Adds a new model to the collection. If autoSync set to true, the newly added model will be synchronized to your Firebase database, triggering an add and sync event both locally and on all other clients. If autoSync is set to false, the add event will only be raised locally.

todoList.add({
  subject: 'Make more coffee',
  importance: 1
});

todoList.on('all', function(event) {
  // if autoSync is true this will log add and sync
  // if autoSync is false this will only log add
  console.log(event);
});

remove(model)

Removes a model from the collection. If autoSync is set to true this model will also be removed from your Firebase database, triggering a remove event both locally and on all other clients. If autoSync is set to false, this model will only trigger a local remove event.

todoList.remove(someModel);

todoList.on('all', function(event) {
  // if autoSync is true this will log remove and sync
  // if autoSync is false this will only log remove
  console.log(event);
});

create(value)

Creates and adds a new model to the collection. The newly created model is returned, along with an id property (uniquely generated by the Firebase client library).

var model = todoList.create({bar: "foo"});
todoList.get(model.id);

todoList.on('all', function(event) {
  // will log add and sync
  console.log(event);
});

Backbone.Firebase.Model

This is a special model object that will automatically synchronize its contents with your Firebase database. You may extend this object, and must provide a Firebase database URL or reference as the url property.

var Todo = Backbone.Firebase.Model.extend({
  url: "https://<your-firebase>.firebaseio.com/mytodo"
});

You may apply query methods as with Backbone.Firebase.Collection.

urlRoot

The urlRoot property can be used to dynamically set the Firebase database reference from the model's id.

var Todo = Backbone.Firebase.Model.extend({
  urlRoot: 'https://<your-firebase>.firebaseio.com/todos'
});

// The url for this todo will be https://<your-firebase>.firebaseio.com/todos/1
var todo = new Todo({
  id: 1
});

You do not need to call any functions that will affect remote data when autoSync is enabled. Calling fetch() will simply fire the sync event.

If autoSync is enabled, you should modify your model as you normally would, (via set() and destroy()) and remote data will be instantly updated.

autoSync: true

var RealtimeModel = Backbone.Firebase.Model.extend({
  url: 'https://<your-firebase>.firebaseio.com/mytodo',
  autoSync: true // true by default
});

var realtimeModel = new RealtimeModel();

realtimeModel.on('sync', function(model) {
  console.log('model loaded', model);
});

// calling .set() will sync the changes to your database
// this will fire the sync, change, and change:name events
realtimeModel.set('name', 'Bob');

autoSync: false

var RealtimeModel = Backbone.Firebase.Model.extend({
  url: 'https://<your-firebase>.firebaseio.com/mytodo',
  autoSync: false
});

var realtimeModel = new RealtimeModel();

realtimeModel.on('sync', function(model) {
  console.log('model loaded', model);
});

// this will fire off the sync event
realtimeModel.fetch();

// calling .save() will sync the changes to your database
// this will fire the sync, change, and change:name events
realtimeModel.save('name', 'Bob');

set(value)

Sets the contents of the model and updates it in your database.

MyTodo.set({foo: "bar"}); // Model is instantly updated in your database (and other clients)

destroy()

Removes the model locally, and from Firebase.

MyTodo.destroy(); // Model is instantly removed from your database (and other clients)

Contributing

If you'd like to contribute to BackboneFire, you'll need to run the following commands to get your environment set up:

$ git clone https://github.com/firebase/backbonefire.git
$ cd backbonefire           # go to the backbonefire directory
$ npm install -g grunt-cli  # globally install grunt task runner
$ npm install -g bower      # globally install Bower package manager
$ npm install               # install local npm build / test dependencies
$ bower install             # install local JavaScript dependencies
$ grunt watch               # watch for source file changes

grunt watch will watch for changes to src/backbonefire.js and lint and minify the source file when a change occurs. The output files - backbonefire.js and backbonefire.min.js - are written to the /dist/ directory.

You can run the test suite via the command line using grunt test.

More Repositories

1

code-prettify

An embeddable script that makes source-code snippets in HTML prettier.
JavaScript
5,768
star
2

chromedeveditor

Chrome Dev Editor is a developer tool for building apps on the Chrome platform - Chrome Apps and Web Apps, in JavaScript or Dart. (NO LONGER IN ACTIVE DEVELOPMENT)
Dart
2,924
star
3

android-Camera2Basic

Migrated:
Java
2,863
star
4

android-ConstraintLayoutExamples

Migrated:
Java
2,571
star
5

firebase-jobdispatcher-android

DEPRECATED please see the README.md below for details.
Java
1,796
star
6

vrview

Library for embedding immersive media into traditional websites.
JavaScript
1,709
star
7

tiger

Java
1,645
star
8

flipjs

A helper library for doing FLIP animations.
JavaScript
1,409
star
9

android-PictureInPicture

Migrated:
Java
1,393
star
10

android-FingerprintDialog

Migrated:
Java
1,375
star
11

observe-js

A library for observing Arrays, Objects and PathValues
JavaScript
1,357
star
12

PyDrive

Google Drive API Python wrapper library
Python
1,304
star
13

js-marker-clusterer

A marker clustering library for the Google Maps JavaScript API v3.
JavaScript
1,279
star
14

android-RuntimePermissions

This sample has been deprecated/archived. Check this repo for related samples:
Java
1,263
star
15

android-Camera2Video

Migrated:
Java
1,201
star
16

chromium-webview-samples

Useful examples for Developing apps with the Chromium based WebView
Java
1,173
star
17

androidtv-Leanback

Migrated:
Java
1,148
star
18

caja

Caja is a tool for safely embedding third party HTML, CSS and JavaScript in your website.
Java
1,126
star
19

android-ui-toolkit-demos

Migrated:
Java
1,118
star
20

android-ScreenCapture

Migrated:
Java
1,021
star
21

android-BluetoothChat

Migrated:
Java
986
star
22

android-BluetoothLeGatt

Migrated:
Java
911
star
23

sample-media-pwa

A sample video-on-demand media Progressive Web App
JavaScript
889
star
24

ChromeWebLab

The Chrome Web Lab for Makers, Hackers and everyone
JavaScript
877
star
25

big-rig

A proof-of-concept Performance Dashboard, CLI and Node module
CSS
857
star
26

android-instant-apps

Migrated:
Java
847
star
27

cloud-functions-emulator

A local emulator for deploying, running, and debugging Google Cloud Functions.
JavaScript
827
star
28

guitar-tuner

A web-based guitar tuner
JavaScript
822
star
29

android-JobScheduler

This sample has been deprecated/archived. Check this repo for related samples:
Java
773
star
30

flashlight

A pluggable integration with ElasticSearch to provide advanced content searches in Firebase.
JavaScript
757
star
31

friendlypix

FriendlyPix is a cross-platform Firebase example app
726
star
32

voice-memos

A Progressive Web App for recording and playing back voice memos.
JavaScript
724
star
33

android-audio-high-performance

We now recommend you use the Oboe libraries:
C++
715
star
34

android-EmojiCompat

Migrated:
Java
707
star
35

android-RecyclerView

Migrated:
Java
675
star
36

ios-swift-chat-example

FireChat implemented in Swift!
Objective-C
673
star
37

leanback-showcase

Migrated:
Java
639
star
38

android-transition-examples

Migrated:
581
star
39

geofire

Realtime location queries with Firebase
569
star
40

drive-music-player

Fully client side Music Player for Google Drive
JavaScript
566
star
41

android-viewpager2

Migrated:
552
star
42

science-journal-ios

Use the sensors in your mobile devices to perform science experiments. Science doesn’t just happen in the classroom or labβ€”tools like Science Journal let you see how the world works with just your phone.
Swift
532
star
43

topeka

quiz app
HTML
532
star
44

android-PdfRendererBasic

Migrated:
Java
522
star
45

science-journal

Use the sensors in your mobile devices to perform science experiments. Science doesn’t just happen in the classroom or labβ€”tools like Science Journal let you see how the world works with just your phone.
Java
508
star
46

tango-examples-java

Example projects for Project Tango [deprecated] Java API
Java
500
star
47

AndroidChat

Demonstrates using the Firebase Android SDK to back a ListView.
Java
499
star
48

android-nearby

Migrated:
Java
494
star
49

android-play-places

Deprecated:
Java
479
star
50

tango-examples-unity

Project Tango [deprecated] UnitySDK Example Projects
C#
475
star
51

easygoogle

Simple wrapper library for Google APIs
Java
471
star
52

firefeed

JavaScript
461
star
53

android-dynamic-features

Migrated:
Kotlin
460
star
54

android-MediaBrowserService

This sample is deprecated.
Java
457
star
55

graphd

The Metaweb graph repository server
C
445
star
56

drive-zipextractor

Extract (decompress) ZIP files into Google Drive using the Google Drive API
JavaScript
435
star
57

android-AutofillFramework

Migrated:
Java
432
star
58

webplatform-samples

HTML5 Samples/Demos
430
star
59

cloud-functions-go

Unofficial Native Go Runtime for Google Cloud Functions
Go
427
star
60

android-unsplash

Deprecated:
424
star
61

android-MultiWindowPlayground

Migrated:
Java
418
star
62

appengine-flask-skeleton

A skeleton for creating Python applications using the Flask framework on App Engine
Python
416
star
63

angularfire-seed

Seed project for AngularFire apps
JavaScript
412
star
64

node-big-rig

A CLI version of Big Rig
JavaScript
410
star
65

firebase-dart

Dart wrapper for Firebase
Dart
409
star
66

android-Camera2Raw

Migrated:
Java
386
star
67

js-store-locator

A library for easily building store-locator-type applications using the Google Maps JavaScript API v3
JavaScript
380
star
68

ADBPlugin

Google Chrome Extension with ADB Daemon
C++
372
star
69

android-fit

Migrated:
Java
372
star
70

android-NotificationChannels

This sample has been deprecated/archived. Check this repo for related samples:
Java
369
star
71

appengine-php-wordpress-starter-project

Starter project for running WordPress on Google Cloud Platform
PHP
368
star
72

android-ActivitySceneTransitionBasic

Migrated:
368
star
73

android-text

Migrated:
Kotlin
363
star
74

android-XYZTouristAttractions

Migrated:
356
star
75

firebase-angular-starter-pack

A Firebase + AngularJS Starter Pack
JavaScript
348
star
76

android-OurStreets

Migrated:
345
star
77

tango-examples-c

JNI example projects for Project Tango [deprecated] C-API
C++
334
star
78

simian

Simian is an enterprise-class Mac OS X software deployment solution. Google App Engine hosted server, with a client powered by the Munki open-source project.
Python
334
star
79

OpenInChrome

Open in Chrome
Objective-C
333
star
80

android-DownloadableFonts

Migrated:
Java
319
star
81

friendlypix-web

FriendlyPix is a cross-platform Firebase example app - This is the web version
JavaScript
312
star
82

pywebsocket

WebSocket server and extension for Apache HTTP Server for testing
Python
310
star
83

android-DarkTheme

migrated:
Java
302
star
84

firereader

Firereader: A feed reader built with Firebase and AngularJS
JavaScript
301
star
85

android-WatchFace

Migrated:
300
star
86

android-MediaRecorder

Migrated:
Java
299
star
87

TemplateBinding

TemplateBinding Prolyfill
JavaScript
291
star
88

firebase-login-demo-android

Java
280
star
89

Firebase-Unity

Objective-C
279
star
90

seed-element

Polymer element boilerplate
HTML
278
star
91

firebase-util

An experimental toolset for Firebase
JavaScript
276
star
92

android-NavigationDrawer

This sample has been deprecated/archived. Check this repo for related samples:
Java
276
star
93

android-DisplayingBitmaps

Migrated:
Java
272
star
94

wReader-app

RSS Reader written using AngularJS
JavaScript
271
star
95

gcm-playground

CSS
268
star
96

android-CardView

Migrated:
Java
265
star
97

soundstagevr

C#
263
star
98

ShadowDOM

ShadowDOM Polyfill
JavaScript
263
star
99

android-credentials

Migrated:
Java
260
star
100

gamebuilder

Game Builder is an application that allows users to create games with little or no coding experience.
C#
253
star