#knockout-firebase Knockout tools for using Firebase as its DB, inspired by FireBind by tyrsius. This is not an offical library from firebase.
- Sync just a value (done)
- Sync an object i.e., a collection of values (done)
- Sync an firebase list (done)
- Sync a value based on the path being an observable i.e., referenced value (done)
- Sync a object based on the path being an observable i.e., referenced object (done)
- Sync a combination of 1-3 (3,1 done, 3,2 done)
- Sync can be read-only or bi-directional (for 1,2,4,5 done)
- Sync can be read-once or continue reading (for 1,2,4,5 done)
There are 89 test currently
This package is designed to be used with a requirejs type project. Include the dist/knockout-firebase file to get all the components
- "knockout" defined for requirejs
- "firebase" defined for requirejs
- requirejs
via bower install knockout-firebase
Fire value is a observable linked to a firebase path.
via coffeescript:
Firebase = require 'firebase'
require 'knockout-firebase'
my_value = ko.fireObservable 'inital_value',
read_only: true
read_once: true
fire_ref: new Firebase('path')
via javascript:
var Firebase, my_value;
Firebase = require('firebase');
require('knockout-firebase');
my_value = ko.fireObservable('inital_value', {
read_only: true,
read_once: true,
fire_ref: new Firebase('path')
});
via coffeescript:
Firebase = require 'firebase'
require 'knockout-firebase'
my_value = ko.observable 'inital_value'
.extend
fireValue:
read_only: true
read_once: true
fire_ref: new Firebase('path')
via javascript:
var Firebase, my_value;
Firebase = require('firebase');
require('knockout-firebase');
my_value = ko.observable('inital_value').extend({
fireValue: {
read_only: true,
read_once: true,
fire_ref: new Firebase('path')
}});
- ko.fireValue is an alias for ko.fireObservable
- read_only if not provided defaults to false
- read_once if not provided defaults to false
- fire_ref can be provided (or changed) later with an optional callback via
# via coffeescript
my_value.Change_Fire_Ref new Firebase('path'), callback
- undefineds are auto replace with null to protect firebase
- if read_only is true, one can still write to the observable but it will not write to firebase
- if read_only is false and firebase has no value (i.e., is null) the inital value will be written to that location
- Use the dispose() function to make sure memory is cleaned up when removing a fireValue
- the observable has a Once_Loaded(callback) function that will call the callback provided once the value has been loaded (like a simple promise)
- Get_Fire_Ref() will return the firebase refrence object
Fire model add fireValues to an object
via coffeescript:
Firebase = require 'firebase'
require 'knockout-firebase'
my_values = {}
keys_and_defaults =
apples: 11
oranges: 12
ko.fireModel my_values, keys_and_defaults,
read_only: true
read_once: true
fire_ref: new Firebase('path')
via javascript:
var Firebase, keys_and_defaults, my_values;
Firebase = require('firebase');
require('knockout-firebase');
my_values = {};
keys_and_defaults = {
apples: 11,
oranges: 12
};
ko.fireModel(my_values, keys_and_defaults, {
read_only: true,
read_once: true,
fire_ref: new Firebase('path')
});
via coffeescript:
Firebase = require 'firebase'
require 'knockout-firebase'
my_values =
apples: ko.observable 11
oranges: ko.observable 12
ko.fireModel my_values, my_values,
read_only: true
read_once: true
fire_ref: new Firebase('path')
via javascript:
var Firebase, my_values;
Firebase = require('firebase');
require('knockout-firebase');
my_values = {
apples: ko.observable(11),
oranges: ko.observable(12)
};
ko.fireModel(my_values, my_values, {
read_only: true,
read_once: true,
fire_ref: new Firebase('path')
});
- first input is the object to attach the fireValues onto
- first input is NOT optional but can be {} as fireModel returns the object back
- first input can already have elements on it and fireModel will not overwrite them but use the observables
- second input is an object of desired keys with their defaults
- read_only if not provided defaults to false
- read_once if not provided defaults to false
- fire_ref is not optional
- fireModel adds fireValues so all of fireValues features can be used on the keys later
- It is possible to mix two different fireModels into the same object and each key will update to the correct place. For example:
# via coffeescript
Firebase = require 'firebase'
require 'knockout-firebase'
my_values =
apples: ko.observable 11
oranges: ko.observable 12
ko.fireModel my_values, {apples: null},
read_only: true
read_once: true
fire_ref: new Firebase('path/stone_fruit')
ko.fireModel my_values, {oranges: null},
read_only: true
read_once: true
fire_ref: new Firebase('path/citrus')
Fire list extends observableArray to support default ordered firebase lists. Default order is no prority by key which is timestamp or creation order.
via coffeescript:
Firebase = require 'firebase'
require 'knockout-firebase'
list = ko.fireList
fire_ref: new Firebase('path')
read_only: true
keys_inits:
type: 'apple'
count: 2
via javascript:
var Firebase, list;
Firebase = require('firebase');
require('knockout-firebase');
list = ko.fireList({
fire_ref: new Firebase('path'),
read_only: true,
keys_inits: {
type: 'apple',
count: 2
}
});
- fire_ref can be provided (or changed) later via
# via coffeescript
my_value.Change_Fire_Ref new Firebase('path')
- keys_inits is an object of firebase keys and default values
- Unlike fireModel the defaults are not written back if the firebase values are null
- One can also extend an observableArray with extend: {fireList: options}
- internally the data is fetch with a once('value') and then kept in sync by other listners.
- internally the objects are NOT fireModels nor fireValues
- dispose() works and will remove any firebase listners
- unlike fireValue and fireModel there is not any options for read_once
- The option read_only, which defaults to false, applies to the keys of each object not push and other list operations
These keys are always present even if keys_inits is empty
- _key - which is the key name of the list object
- _ref - which is the firebase reference of the list object
- push, remove, splice, and shift all work
- unshift, reverse, sort, removeAll, destroy, destroyAll do NOT work
Fire Value that switchs firebase paths based on an observable
via coffeescript:
Firebase = require 'firebase'
require 'knockout-firebase'
my_key = ko.observable 'path'
my_value = ko.fireValueByRef 'inital_value',
read_only: true
read_once: true
fire_ref: new Firebase('path')
ref_obs_id: my_key
via javascript:
var Firebase, my_key, my_value;
Firebase = require('firebase');
require('knockout-firebase');
my_key = ko.observable('path');
my_value = ko.fireValueByRef('inital_value', {
read_only: true,
read_once: true,
fire_ref: new Firebase('path'),
ref_obs_id: my_key
child_path: 'child'
});
- just like FireValue expect fire_ref and ref_obs_id are not optional
- if ref_obs_id() is null or undefined it will disconnect from firebase
- the path of the value is fire_ref + ref_obs_id() + child
- child_path is optional
- every time ref_obs_id changes it will re-read the value from firebase (even if read_once is true)
- fireValueByRef return a fireValue object
Fire Model that switchs firebase paths based on an observable
via coffeescript:
Firebase = require 'firebase'
require 'knockout-firebase'
my_values =
apples: ko.observable 11
oranges: ko.observable 12
my_key = ko.observable 'path'
ko.fireModelByRef my_values, my_values,
read_only: true
read_once: true
fire_ref: new Firebase('path')
ref_obs_id: my_key
child_path: 'path'
via javascript:
var Firebase, my_values, my_key;
Firebase = require('firebase');
require('knockout-firebase');
my_values = {
apples: ko.observable(11),
oranges: ko.observable(12)
};
my_key = ko.observable('path');
ko.fireModelByRef(my_values, my_values, {
read_only: true,
read_once: true,
fire_ref: new Firebase('path'),
ref_obs_id: my_key,
child_path: 'path'
});
- just like FireModel expect ref_obs_id is added and not optional
- if ref_obs_id() is null or undefined it will disconnect from firebase
- the path of the value is fire_ref + ref_obs_id() + child + keys
- child_path is optional
- every time ref_obs_id changes it will re-read the value from firebase
- FireModelByRef return a FireModel object
#License Copyright © 2015 Curtis M. Humphrey and is licensed under the terms of the MIT License.