npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@risd/emberfire

v1.4.1

Published

Ember binding for Firebase for use with @risd/webhook-cms

Downloads

1

Readme

EmberFire (Firebase + Ember Data)

Build Status Version

EmberFire is the officially supported adapter for using Firebase with Ember Data.

The DS.FirebaseAdapter provides all of the standard DS.Adapter methods and will automatically synchronize the store with Firebase.

If you would like to use Firebase without Ember Data, we recommend the third-party ember-firebase binding.

Join the Firebase + Ember Google Group to ask technical questions, share apps you've built, and chat with other developers in the community.

Downloading EmberFire

In order to use EmberFire in your project, you need to include the following files in your HTML:

<!-- Ember + Ember Data -->
<script src="http://builds.emberjs.com/tags/v1.9.1/ember.js"></script>
<script src="http://builds.emberjs.com/tags/v1.0.0-beta.12/ember-data.js"></script>

<!-- Firebase -->
<script src="https://cdn.firebase.com/js/client/2.0.6/firebase.js"></script>

<!-- EmberFire -->
<script src="https://cdn.firebase.com/libs/emberfire/1.3.3/emberfire.min.js"></script>

Use the URL above to download both the minified and non-minified versions of EmberFire from the Firebase CDN. You can also download them from the releases page of this GitHub repository. Firebase and Ember can be downloaded directly from their respective websites.

You can also install EmberFire via Bower and its dependencies will be downloaded automatically:

$ bower install emberfire --save

Getting Started with Firebase

EmberFire requires Firebase in order to sync data. You can sign up here for a free account.

Usage

To get started, simply create an instance of the DS.FirebaseAdapter in your app:

App.ApplicationAdapter = DS.FirebaseAdapter.extend({
  firebase: new Firebase("https://<your-firebase>.firebaseio.com")
});

Your Firebase data will now be synced with the Ember Data store.

You can now interact with the data store as you normally would. For example, calling find() with a specific ID will retrieve that record from Firebase. Additionally, from that point on, every time that record is updated in Firebase, it will automatically be updated in the local data store.

See the Ember documentation for a full list of methods, including ways to create, find, delete and query records.

Ember CLI

EmberFire also works with the Ember CLI Example App. Run the following command to add emberfire.js to your project:

Temporary install instructions for ember-cli:

$ npm install emberfire --save-dev
$ ember generate emberfire

Then, all you need to do is create app/adapters/application.js with the following content:

/* globals Firebase */

export default DS.FirebaseAdapter.extend({
  firebase: new Firebase("https://<your-firebase>.firebaseio.com")
});

Data Structure

By default, EmberFire will try to determine the correct Firebase reference based on the model name.

// Define a Post model
App.Post = DS.Model.extend();

// Records will be fetched from to https://<your-firebase>.firebaseio.com/posts
var posts = store.findAll("post");

// The new record will be saved to https://<your-firebase>.firebaseio.com/posts/post_id
var newPost = store.createRecord("post").save();

What if my data is named differently?

If you would like to customize where a model will be fetched/saved, simply create a model-specific adapter:

// Define a Post model
App.Post = DS.Model.extend();

// Define a Post adapter
App.PostAdapter = App.ApplicationAdapter.extend({
  pathForType: function(type) {
    return "custom-posts";
  }
});

Overriding the pathForType() method will allow you to tell the adapter where it should fetch/save records of the specified type.

// Records will now be fetched from to https://<your-firebase>.firebaseio.com/custom-posts
var posts = store.findAll("post");

// The new record will now be saved to https://<your-firebase>.firebaseio.com/custom-posts/post_id
var newPost = store.createRecord("post").save();

Relationships

EmberFire can handle relationships in two different ways: async and embedded.

Async

Any relationship that is flagged as async: true tells the adapter to fetch the record if it hasn't already been loaded.

App.Post = DS.Model.extend({
  comments: DS.hasMany("comment", { async: true })
});

App.Comment = DS.Model.extend({
  post: DS.belongsTo("post", { async: true })
});

In the App.Post example, comments will be fetched from https://<your-firebase>.firebaseio.com/comments. Here is what the data structure would look like in Firebase:

{
  "posts": {
    "post_id_1": {
      "comments": {
        "comment_id_1": true
      }
    }
  },
  "comments": {
    "comment_id_1": {
      "body": "This is a comment",
      "post": "post_id_1"
    }
  }
}

Note: If your async data isn't auto-loading, make sure you've defined your relationships in both directions.

Embedded

Any relationship that is flagged as embedded: true tells the adapter that the related records have been included in the payload.

Generally, this approach is more complicated and not as widely used, but it has been included to support existing data structures.

hasMany()
App.Post = DS.Model.extend({
  comments: DS.hasMany("comment", { embedded: true })
});

Here is what the data structure would look like in Firebase:

{
  "posts": {
    "post_id_1": {
      "comments": {
        "comment_id_1": {
          "body": "This is a comment"
        }
      }
    }
  }
}

Note: When a model has embedded relationships, the related model should not be saved on its own.

var comment = store.createRecord("comment");
// This WILL NOT save the comment inside of the post because the adapter doesn't know
// where to save the comment without the context of the post
comment.save();

Instead, the comment needs to be added to the post and then the post can be saved:

// Add the new comment to the post and save it
post.get("comments").addObject(comment);

// Saving the post will save the embedded comments
post.save();
belongsTo()

Any embedded belongsTo() relationship must specify an id property in the payload:

{
  "posts": {
    "post_id_1": {
      "user": {
        "id": "myusername"
      }
    }
  }
}

Contributing

If you'd like to contribute to EmberFire, you'll need to run the following commands to get your environment set up:

$ git clone https://github.com/firebase/emberfire.git
$ cd emberfire              # go to the emberfire directory
$ npm install -g grunt-cli  # globally install grunt task runner
$ npm install -g bower      # globally install Bower package manager
$ npm install               # install local npm build / test dependencies
$ bower install             # install local JavaScript dependencies
$ grunt watch               # watch for source file changes

grunt watch will watch for changes in the /src/ directory and lint, concatenate, and minify the source files and run the test suite when a change occurs. The output files - emberfire.js and emberfire.min.js - are written to the /dist/ directory.

You can run the test suite by navigating to file:///path/to/emberfire/test/index.html or via the command line using grunt test.