- Notifications
You must be signed in to change notification settings - Fork0
A data persistence library for Ember.js.
License
nightire/data
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Ember Data is a library for robustly managing model data in yourEmber.js applications.
Ember Data is designed to be agnostic to the underlying persistencemechanism, so it works just as well with JSON APIs over HTTP as it doeswith streaming WebSockets or local IndexedDB storage.
It provides many of the facilities you'd find in server-side ORMs likeActiveRecord, but is designed specifically for the unique environment ofJavaScript in the browser.
In particular, Ember Data uses Promises/A+-compatible promises from theground up to manage loading and saving records, so integrating withother JavaScript APIs is easy.
bower install ember-data --save
The latest passing build from the "master" branch is available onhttp://emberjs.com/builds/#/canary.
Similarly, the latest passing build from the "beta" branch can be foundonhttp://emberjs.com/builds/#/beta
Or build ember-data.js yourself. Clone the repository and runnpm run build:production
aftersetup. You'll find ember-data.js in thedist
directory.
Internet Explorer 8 support requires Ember 1.8.1 (which provides a polyfill forObject.create
).
In Ember Data, thestore is responsible for managing the lifecycle ofyour 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 theEmber Data library, all of the routes and controllers in yourapplication will get a newstore
property. This property is aninstance ofDS.Store
that will be shared across all of the routes andcontrollers in your app.
First thing's first: tell Ember Data about the models in yourapplication. For example, imagine we're writing a blog reader app.
Here's what your model definition would look like if you're usingES6 modules (via ember-cli):
// app/models/blog-post.jsimportDSfrom'ember-data';exportdefaultDS.Model.extend({title:DS.attr('string'),createdAt:DS.attr('date'),comments:DS.hasMany('comment')});// app/models/comment.jsimportDSfrom'ember-data';exportdefaultDS.Model.extend({body:DS.attr('string'),username:DS.attr('string'),post:DS.belongsTo('blog-post')});
If you're using globals (that is, not something like ember-cli), yourmodels would look like this:
varattr=DS.attr;varhasMany=DS.hasMany;varbelongsTo=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')});
Without immediately diving in to the depths of the architecture, onething youshould know is that Ember Data uses an object called anadapter to know how to talk to your server.
An adapter is just an object that knows how to translate requests fromEmber Data into requests on your server. For example, if I ask the EmberData store for a record of typeperson
with an ID of123
, theadapter translates that into an XHR request to (for example)api.example.com/v3/person/123.json
.
By default, Ember Data will use theRESTAdapter
, which adheres to aset of RESTful JSON conventions.
To learn more about adapters, including what conventions theRESTAdapter
follows and how to build your own, see the Ember.jsGuides:Connecting to an HTTPServer.
From your route or controller:
this.store.findAll('blog-post');
This returns a promise that resolves to the collection of records.
this.store.findRecord('blog-post',123);
This returns a promise that resolves to the requested record. If therecord can't be found or there was an error during the request, thepromise will be rejected.
For much more detail on how to use Ember Data, see theEmber.js Guideson models.
- Ensure thatNode.js is installed.
- Run
npm install
to ensure the required dependencies are installed. - Run
npm run build:production
to build Ember Data. The builds will be placed in thedist/
directory.
Install Node.js fromhttp://nodejs.org or your favorite package manager.
Install Ember CLI and bower.
npm install -g ember-cli bower
Run
npm install
inside the project root to install the JS dependencies.
To start the development server, run
npm start
.
Install phantomjs fromhttp://phantomjs.org
Run
npm test
About
A data persistence library for Ember.js.
Resources
License
Code of conduct
Stars
Watchers
Forks
Packages0
Languages
- JavaScript99.3%
- Other0.7%