One of the cool features in AngularJS is the two-way data binding. When you change a property of an object, the value is instantly changed in every view or service that is using this property. A while ago, I saw a video about the combination of AngularJS and Firebase. This combination makes it possible to create three-way data binding in AngularJS.
What is Firebase?
Firebase is an online service that provides an API to store and sync data in realtime. This means that when the data is saved, all connected clients will retrieve the latest data without reloading the page. You can see Firebase as a database service with some extra features like authentication. The cool thing about this service is that you don’t need to create a database schema. You can create collections just by specifying a path to the database in the URL. An additional package ‘AngularFire’ is available for integration with AngularJS.
The world’s easiest guestbook
Getting started
A good way to start a test project is by using Yeoman. Both AngularJs and AngularFire have a generator that will create your basic application in no time. Just run following commands and follow the wizard:
The view
Initializing your firebase collection
The AngularFire package provides some ways to interact with Firebase. The easiest way to connecto to a collection is by using the syncData service. We can create the connection with one line of code:
Add a message
Now that we set up the connection, we want to add messages to the collection. This part is very easy: you just pass a JSON object to the collection:
Sort messages in reverse order
When you want to display the messages in reverse order you should create one extra filer called reverse
. This filter is allready written in one of the example AngularFire projects. You can find the code over here.
A quick way to create a custom filter is by using the Yeoman angular:filter generator:
Hosting
It is also possible to use Firebase hosting. A custom CLI tool is provided an can be installed through NPM:
The next step is to create a firebase configuration file. This can be done with the command:
Note: The destination folder should be dist
The next step is to deploy the application. This is also very easy:
The sample application is now available at: Firebase App.