Coder Social home page Coder Social logo

bennlich / firebase.getasarray Goto Github PK

View Code? Open in Web Editor NEW

This project forked from katowulf/firebase.getasarray

0.0 1.0 0.0 26 KB

A simple library to demonstrate how arrays can be synchronized to a real-time, distributed system like Firebase.

JavaScript 100.00%

firebase.getasarray's Introduction

Firebase.getAsArray

A simple library to demonstrate how arrays can be synchronized to a real-time, distributed system like Firebase.

This library demonstrates the following best practices for using arrays with collaborative, real-time data:

  • make the array read only (don't use splice, pop, etc; have setter methods)
  • when possible, refer to records using a uniqueId rather than array index
  • synchronize changes from the server directly into our array
  • push local changes to the server and letting them trickle back

In other words, our array is essentialy one-directional. Changes come from the server into the array, we read them out, we push our local edits to the server, they trickle back into the array.

Read more about synchronized arrays and this lib on the Firebase Blog.

Installation

Download the firebase-as-array.min.js file and include it in your HTML:

<script src="firebase-as-array.js"></script>
<script>
   var ref = new Firebase(URL);
   var list = Firebase.getAsArray(ref);
</script>

Or in your node.js project:

var Firebase = require('firebase');
var getAsArray = require('./firebase-as-array.js');

var ref = new Firebase(URL);
var list = getAsArray(ref);

Usage

var ref = new Firebase(URL);

// create a synchronized array
var list = getAsArray(ref);

// add a new record
var ref = list.$add({foo: 'bar'});

// remove record
list.$remove( key );

// set priority on a record
list.$move( key, newPriority );

// find position of a key in the list
list.$indexOf( key );

// find key for a record at position 1
list[1].$id;

Limitations

All the records stored in the array are objects. Primitive values get stored as { '.value': primitive }

Does not support IE 8 and below by default. To support these, simply include polyfills for Array.prototype.forEach and Function.prototype.bind in your code base.

API

getAsArray(ref[, eventCallback])

@param {Firebase} ref
@param {Function} [callback]
@returns {Array}

Creates a new array and synchronizes it to the ref provided.

$id

The record ID. This is the unique URL key used to store the record in Firebase (the equivalent of firebaseRef.name()).

$indexOf(key)

@param {String} key the path name for a record to find in the array
@returns {int} the index of the element in the array or -1 if not found

A convenience method to find the array position of a given key.

$add(data)

@param data  the data to be put in Firebase as a new child record
@returns {Firebase} the ref to the newly created record

Adds a record to Firebase and returns the reference. To obtain its id, use ref.name(), assuming ref is the variable assigned to the return value.

$remove(key)

@param {string} key a record id to be removed locally and remotely

Removes a record locally and from Firebase

$set(key, data)

@param {string} key a record id to be replaced
@param data what goes into it

Replaces the value of a record locally and in Firebase

$update(key, data)

@param {string} key a record id to be updated
@param {object} data some keys to be replaced

Updates the value of a record locally, replacing any keys that are in data with the values provided and leaving the rest of the record alone.

$move(key, newPriority)

@param {string} key record id to be moved
@param {string|int} newPriority the sort order to be applied

Moves a record locally and in the remote data list.

Development

This lib is intended primarily to be an example. However, pull requests will be happily accepted.

git clone [email protected]:yourname/Firebase.getAsArray
npm install
grunt
grunt watch
# make your fixes
# verify tests are at 100%
grunt make
git commit -m "your changes"
git push
# create a pull request!

Support

Use the issue tracker if you have questions or problems. Since this is meant primarily as an example, do not expect prompt replies!

firebase.getasarray's People

Contributors

bennlich avatar katowulf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.