A data persistence library for Ember.js.
JavaScript Other
Latest commit bc9a659 Dec 2, 2016 @pangratz pangratz committed on GitHub Merge pull request #4691 from Turbo87/mocha-blueprints
[BUGFIX beta] Add blueprints for "ember-cli-mocha >= 0.12.0"
Permalink
Failed to load latest commit information.
addon Fixed a typo Dec 1, 2016
app Removes Ember.K from codebase (#4670) Nov 20, 2016
benchmarks bump dep Sep 30, 2016
bin Revert "Uses bin/start with friendly error message for npm start." Jun 2, 2016
blueprints [BUGFIX beta] Add blueprints for "ember-cli-mocha >= 0.12.0" Dec 2, 2016
config Merge pull request #4686 from pangratz/disable-ds-improved-ajax Nov 30, 2016
docs Merge pull request #1720 from rjackson/add-docs-task Jan 29, 2014
generators Happy New Year! Jan 1, 2016
lib perf(internal-model): lazily allocation internal state. Oct 19, 2016
node-tests [BUGFIX beta] Add blueprints for "ember-cli-mocha >= 0.12.0" Dec 2, 2016
server fix mock server Nov 18, 2016
tests Merge pull request #4685 from pangratz/ds-rollback-attribute Nov 30, 2016
vendor adds a mock server, dummy app, and heimdall instrumentation for bench… Sep 30, 2016
.bowerrc fix bower related build issues in .travis.yml Nov 6, 2015
.codeclimate.yml [skip ci] fix invalid indentation in codeclimate.yml Apr 7, 2016
.editorconfig bump to latest ember-cli, run ember init, cleanup post init (#4536) Sep 16, 2016
.ember-cli update ember-cli to 1.13.12 Nov 14, 2015
.eslintrc.js adds a mock server, dummy app, and heimdall instrumentation for bench… Sep 30, 2016
.gitignore adds a mock server, dummy app, and heimdall instrumentation for bench… Sep 30, 2016
.npmignore bump to latest ember-cli, run ember init, cleanup post init (#4536) Sep 16, 2016
.travis.yml try busting the cache by keeping phantomjs info in npm (#4528) Sep 9, 2016
.watchmanconfig update ember-cli to 1.13.12 Nov 14, 2015
CHANGELOG.md Update changelog for the Ember Data 2.10.0 release Nov 28, 2016
CODE_OF_CONDUCT.md Add link to community guidelines as CODE_OF_CONDUCT.md. Jun 20, 2015
CONTRIBUTING.md adds note on benchmarking to the readme Sep 30, 2016
FEATURES.md [FEATURE ds-rollback-attribute] rename ds-reset-attribute Nov 30, 2016
LICENSE Happy New Year! Jan 1, 2016
README.md [DOC] Update Readme to reflect new default adapter Oct 15, 2016
RELEASE.md Update RELEASE.md Mar 22, 2016
appveyor.yml Run tests in production environment Mar 10, 2016
bower.json bump to latest ember-cli, run ember init, cleanup post init (#4536) Sep 16, 2016
ember-cli-build.js perf(internal-model): lazily allocation internal state. Oct 19, 2016
ember-data-source.gemspec [BUGFIX release] Add missing dist source to gem Jan 13, 2016
index.js safer toggling of instrumentation Sep 30, 2016
package.json Bump version for 2.12.0 canary Nov 28, 2016
testem.js Replace JSHint/JSCS with ESLint (#4360) May 24, 2016

README.md

Ember Data Build Status Code Climate

Ember Data is a library for robustly managing model data in your Ember.js applications.

Ember Data is designed to be agnostic to the underlying persistence mechanism, so it works just as well with JSON APIs over HTTP as it does with streaming WebSockets or local IndexedDB storage.

It provides many of the facilities you'd find in server-side ORMs like ActiveRecord, but is designed specifically for the unique environment of JavaScript in the browser.

In particular, Ember Data uses Promises/A+-compatible promises from the ground up to manage loading and saving records, so integrating with other JavaScript APIs is easy.

Igor Terzic is currently the lead maintainer of Ember Data, while the rest of the core team include Yehuda Katz, Tom Dale, Brendan McLoughlin, Christoffer Persson and Stanley Stuart.

Using Ember Data

Getting Ember Data

Since version 2.3 ember-data is a proper Ember-CLI addon which can be added to your app via:

ember install ember-data

If you need to use a version of ember-data package < 2.3, you need to add the npm package and add the dependency via bower:

npm install ember-data@v2.2.1 --save-dev
bower install ember-data --save

The latest passing build from the "master" branch is available on http://emberjs.com/builds/#/canary.

Similarly, the latest passing build from the "beta" branch can be found on http://emberjs.com/builds/#/beta

Or build ember-data.js yourself. Clone the repository and run npm run build:production after setup. You'll find ember-data.js in the dist directory.

Internet Explorer 8

Internet Explorer 8 is no longer supported by Ember Data on versions 2.0 and later.

If you require IE8 support, you can use the 1.13 series of releases. The source code is available on the release-1-13 branch.

Internet Explorer 8 support requires Ember 1.8.1 (which provides a polyfill for Object.create).

Instantiating the Store

In Ember Data, the store is responsible for managing the lifecycle of your models. Every time you need a model or a collection of models, you'll ask the store for it.

To create a store, you don't need to do anything. Just by loading the Ember Data library, all of the routes and controllers in your application will get a new store property. This property is an instance of DS.Store that will be shared across all of the routes and controllers in your app.

Defining Your Models

First thing's first: tell Ember Data about the models in your application. For example, imagine we're writing a blog reader app.

Here's what your model definition would look like if you're using ES6 modules (via ember-cli):

// app/models/blog-post.js
import DS from 'ember-data';

const { attr, hasMany } = DS;

export default DS.Model.extend({
  title: attr('string'),
  createdAt: attr('date'),

  comments: hasMany('comment')
});

// app/models/comment.js
import DS from 'ember-data';

const { attr, belongsTo } = DS;

export default DS.Model.extend({
  body: attr('string'),
  username: attr('string'),

  post: belongsTo('blog-post')
});

If you're using globals (that is, not something like ember-cli), your models would look like this:

var attr = DS.attr;
var hasMany = DS.hasMany;
var belongsTo = DS.belongsTo;

App.BlogPost = DS.Model.extend({
  title: attr('string'),
  createdAt: attr('date'),

  comments: hasMany('comment')
});

App.Comment = DS.Model.extend({
  body: attr('string'),
  username: attr('string'),

  post: belongsTo('blog-post')
});

A Brief Note on Adapters

Without immediately diving in to the depths of the architecture, one thing you should know is that Ember Data uses an object called an adapter to know how to talk to your server.

An adapter is just an object that knows how to translate requests from Ember Data into requests on your server. For example, if I ask the Ember Data store for a record of type person with an ID of 123, the adapter translates that into an XHR request to (for example) api.example.com/v3/person/123.json.

By default, Ember Data will use the JSONAPIAdapter, which adheres to the JSON-API spec.

To learn more about adapters, including what conventions the various adapters follow and how to build your own, see the Ember.js Guides: Customizing Adapters.

Fetching a Collection of Models

From your route or controller:

this.store.findAll('blog-post');

This returns a promise that resolves to the collection of records.

Fetching a Single Model

this.store.findRecord('blog-post', 123);

This returns a promise that resolves to the requested record. If the record can't be found or there was an error during the request, the promise will be rejected.

Even More Documentation

For much more detail on how to use Ember Data, see the Ember.js Guides on models.

Building Ember Data

  1. Ensure that Node.js is installed.
  2. Run npm install to ensure the required dependencies are installed.
  3. Run npm run build:production to build Ember Data. The builds will be placed in the dist/ directory.

Contribution

See CONTRIBUTING.md

How to Run Unit Tests

Setup

  1. Install Node.js from http://nodejs.org or your favorite package manager.

  2. Install Ember CLI and bower. npm install -g ember-cli bower

  3. Run npm install inside the project root to install the JS dependencies.

In Your Browser

  1. To start the development server, run npm start.

  2. Visit http://localhost:4200/tests

From the CLI

  1. Install phantomjs from http://phantomjs.org

  2. Run npm test