Firebase Firebase
  • Products
  • Use Cases
  • Pricing
  • Docs
  • Support
  • Home
  • Products
  • Use Cases
  • Pricing
  • Docs
  • Support

The Firebase Blog

Announcing New Bindings for EmberJS

March 21, 2014
Adam Putinski
Senior Frontend Developer

Just in time for EmberConf next week, we’re excited to announce a new version of our EmberFire bindings, along with an example blogging app to showcase the magic of using Firebase with Ember.

Our new Ember bindings now work directly with Ember Data. This means you can now use Ember Data’s relationships with Firebase to define hasMany and belongsTo relationships between your models. If you’re new to Ember Data, Ember’s guide to models is a great place to start.

With these updates, EmberFire now works only with Ember Data. For Ember developers who aren’t using Ember Data, Michael Jackson has written ember-firebase - an awesome, thoroughly tested set of Firebase bindings for Ember. If you’d still like to use the previous version of EmberFire, it is available in the v0.1.0 release on GitHub.

Getting Started With the New EmberFire

With our new bindings, you can start using EmberFire in just two steps. First, you’ll need to include the necessary libraries:


  
  
  

  
  

  
  

You can also download EmberFire and all its dependencies from Bower via bower install emberfire --save.

Then, simply create an instance of the DS.FirebaseAdapter in your app and add your Firebase URL:

App.ApplicationAdapter = DS.FirebaseAdapter.extend({
  firebase: new Firebase('https://.firebaseio.com')
});

With the adapter set up, you can now interact with the data store as you normally would with Ember. For example, calling find() with a specific ID will retrieve that record from Firebase. It will also start watching for updates and will update the data store automatically whenever anything is added or removed.

FireBlog

To help you understand our new EmberFire bindings we’ve created FireBlog, a realtime blogging app with posts, comments, and users. We’ll walk through the core concepts here, and the full code is available on GitHub.



Structuring Data in EmberFire

Because both Ember Data and Firebase work best with denormalized data, FireBlog does not embed comment data directly in each post. Instead, each post has a comments object with a comment ID pointing to the comment data in /comments. We’ve created three top-level keys for posts, comments, and users, and the data structure in Firebase looks like this:



Setting Up a Model

Each post in our blog will have a title, body, published, user, and comments attributes. Using Ember Data’s relationships, each post belongsTo a user and hasMany comments. We’ll use Ember’s computed properties to format the date to display on the blog post.

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  body: DS.attr('string'),
  published: DS.attr('number'),
  publishedDate: function() {
    return moment(this.get('published')).format('MMMM Do, YYYY');
  }.property('published'),
  user: DS.belongsTo('user', { async: true }),
  comments: DS.hasMany('comment', { async: true })
});

The comment and user models are set up in a similar way, which you can see on GitHub.

Fetching All Posts

To get all blog posts from Firebase we can use the standard Ember Data way of fetching posts, like the following:

App.PostsIndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.findAll('post');
  }
});

This will fetch all posts when the page first loads, and will also listen for updates to Firebase. Whenever posts are added, the data store will update automatically and new posts will be displayed in the DOM.

Displaying a Single Post

Similar to how we fetched all posts using store.findAll(), we’ll fetch individual posts using store.find():

App.PostRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('post', params.post_id);
  }
});

We'll use the following template to display a single post. Then we'll create an Ember.Component to display individual blog posts. We'll pass in the model from the route (post=model) as a property to show the post's title, author, date, and comments:


Here is the App.FirePostComponent we use to display an individual blog post:


Now we have a full-featured blogging application that displays posts and comments in realtime without a server. It’s that simple!

Send Us Your Feedback

Our goal with the new EmberFire library is to make the process of adding a backend to your Ember app and integrating with Ember Data as seamless as possible. We encourage you to take our new bindings for a spin. Submit a pull request or email me at [email protected] with any feedback. We’re just getting started with this new Ember Data integration, and we’re working hard to make updates based on your suggestions. We’re excited to see what you build with Firebase and Ember.

For those of you going to EmberConf, look for James, Alex, Sara and I in the bright yellow t-shirts!

Share on Twitter Share on Facebook
Google
  

About

Firebase gives you the tools and infrastructure to build better apps and grow successful businesses. Learn more

Popular Posts

  • Introducing Cloud Firestore: Our New Document Database for Apps
  • Best Practices: Arrays in Firebase
  • The beginners guide to React Native and Firebase
  • Firebase expands to become a unified app platform
  • Email Verification in Firebase Auth

Archive

  • ►  2022 (5)
    • ►  March (2)
    • ►  February (2)
    • ►  January (1)
  • ►  2021 (41)
    • ►  November (3)
    • ►  October (4)
    • ►  September (4)
    • ►  August (7)
    • ►  July (6)
    • ►  June (4)
    • ►  May (3)
    • ►  April (2)
    • ►  March (2)
    • ►  February (3)
    • ►  January (3)
  • ►  2020 (39)
    • ►  December (1)
    • ►  November (4)
    • ►  October (8)
    • ►  September (4)
    • ►  August (6)
    • ►  July (3)
    • ►  June (4)
    • ►  May (2)
    • ►  March (3)
    • ►  February (1)
    • ►  January (3)
  • ►  2019 (31)
    • ►  December (1)
    • ►  November (4)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (1)
    • ►  May (1)
    • ►  April (4)
    • ►  March (7)
    • ►  February (2)
    • ►  January (2)
  • ►  2018 (55)
    • ►  December (3)
    • ►  November (8)
    • ►  October (1)
    • ►  September (6)
    • ►  August (9)
    • ►  July (1)
    • ►  June (5)
    • ►  May (5)
    • ►  April (4)
    • ►  March (7)
    • ►  February (2)
    • ►  January (4)
  • ►  2017 (86)
    • ►  December (10)
    • ►  November (6)
    • ►  October (5)
    • ►  September (6)
    • ►  August (9)
    • ►  July (6)
    • ►  June (7)
    • ►  May (8)
    • ►  April (5)
    • ►  March (10)
    • ►  February (9)
    • ►  January (5)
  • ►  2016 (66)
    • ►  December (6)
    • ►  November (11)
    • ►  October (9)
    • ►  September (9)
    • ►  August (6)
    • ►  July (6)
    • ►  June (7)
    • ►  May (3)
    • ►  April (1)
    • ►  March (1)
    • ►  February (4)
    • ►  January (3)
  • ►  2015 (24)
    • ►  December (2)
    • ►  November (1)
    • ►  October (4)
    • ►  September (1)
    • ►  August (2)
    • ►  July (2)
    • ►  May (4)
    • ►  April (1)
    • ►  March (5)
    • ►  February (1)
    • ►  January (1)
  • ▼  2014 (29)
    • ►  December (3)
    • ►  November (3)
    • ►  October (3)
    • ►  September (1)
    • ►  August (2)
    • ►  July (2)
    • ►  June (1)
    • ►  May (4)
    • ►  April (4)
    • ▼  March (2)
      • Announcing Streaming for the Firebase REST API
      • Announcing New Bindings for EmberJS
    • ►  February (1)
    • ►  January (3)
  • ►  2013 (28)
    • ►  December (3)
    • ►  November (2)
    • ►  October (2)
    • ►  September (1)
    • ►  August (2)
    • ►  July (2)
    • ►  June (3)
    • ►  May (1)
    • ►  April (3)
    • ►  March (6)
    • ►  February (2)
    • ►  January (1)
  • ►  2012 (8)
    • ►  December (2)
    • ►  October (1)
    • ►  September (1)
    • ►  July (1)
    • ►  June (1)
    • ►  April (2)

Recent Posts

Twitter Facebook YouTube
  • Learn

    • Guides
    • Reference
    • Samples
    • Libraries
    • GitHub
  • Stay Connected

    • Blog
    • Facebook
    • Google+
    • Twitter
    • YouTube
  • Support

    • Contact Support
    • Stack Overflow
    • Google Group
    • Release Notes
    • Known Issues
    • Sign In to Legacy Console
    • Android
    • Chrome
    • Google Cloud Platform
    • All Products