Distributed via
Firebase binding use $q in AngularJS
LiveScript is a compile-to-js language, which provides us more robust way to write JavaScript.
It also has great readibility and lots of syntax sugar just like you're writting python/ruby.
We use newly introduced $q API to notify
you about the value/order changes, we let you decide how you deal with your data with $scope
.
We know that you want to take advantage of collection in Firebase
, but still want to preserve the right order, or order by any properties in each item. ng-fire-alarm
allows to transform collection object into native js array for you.
We follow seperation of concerns by seperating control(reference) object and data source to Alarm
object and Fire
object(s). So a ng-change
, ng-submit
, ng-click
can be directly bound to Alarm object, while your Fire object(s) are clean and just like what you see in Firebase dashboard.
- Download and include
ng-fire-alarm.js
ORng-fire-alarm.min.js
.
Then include them through script tag in your HTML.
Add this line to your application's Gemfile:
gem 'ng-fire-alarm'
And then execute:
$ bundle
Then add these lines to the top of your app/assets/javascripts/application.js
file:
//= require angular
//= require ng-fire-alarm
And include in your angular
module definition:
var module = angular.module('my-awesome-project', ['ng-fire-alarm']).
We add a new method to Firebase.prototype
:
The key to transform your Firebase
reference into a AngularJS
powered alarm object, it take one parameter:
Pass true will transform collection object into native js array for you.
A wrapped object over Firbease reference that is returned by calling Firebase.prototype.$toAlarm
.
$promise
: it will be notified everytime the value/order changes.
$thenNotify
: Same as$promise.then(void, void, callback)
.
Register a callback that notify you each time the alarm rings.
Unlike$promise.then
, this method is self-chained and will return alarm object to allow chaining.
See examples below.
Ther're wrapper for Firebase.prototype.limit/startAt/endAt
function, but it'll update internal Firebase reference and it'll populate new data through your callback registered via $thenNotify
.
Examples:
var ROOT = new Firebase('https://ng-fire-alarm.firebaseio.com/');
function UsersListCtrl ($scope) {
$scope.users = [];
$scope.usersAlarm = ROOT.child('users').$toAlarm({collection: true});
$scope.usersAlarm.$thenNotify(function(users) {
$scope.users = users;
});
}
Fast prototyping!
<ul ng-controller="UsersListCtrl" infinite-scroll="usersAlarm.$limit(users.length + 25)">
<li ng-repeat="user in users">
{{ user.name }}
</li>
</ul>
They're wrapper for Firebase.prototype.remove/push/update/set/setPriority/setWithPriority
function, but it'll return a promise
object instead of passing in a callback function.
Examples:
var ROOT = new Firebase('https://ng-fire-alarm.firebaseio.com/');
function UserEditCtrl ($scope) {
$scope.userAlarm = ROOT
.child('users/`')
.$toAlarm()
.$thenNotify(function(user) { $scope.user = user; });
}
Fast prototyping!
<form ng-controller="UserEditCtrl">
<input type="text" ng-model="user.name" ng-change="userAlarm.$update(user)">
</form>
Object that is passed in to callbacks registered via $thenNotify
, they can be primitive, object, or array:
Primitive is just js primitive. There's NO NO NO wrapper around primitive .{$value: primitive}
bell.$thenNotify(function (aStringOrANumber) { $scope.myVar = aStringOrANumber; });
we've add two properties on it:
sorted by native Firebase ordering.
Each item in array, if they're object, will have these properties:
<div ng-repeat="item in array | orderBy:'$index':true"></div>
- Fork it ( https://github.com/tomchentw/ng-fire-alarm/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request