Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
forked fromemberjs/data

A data persistence library for Ember.js.

License

NotificationsYou must be signed in to change notification settings

nightire/data

 
 

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.

Using Ember Data

Getting Ember Data

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:productionaftersetup. You'll find ember-data.js in thedist directory.

Internet Explorer 8

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

Instantiating the Store

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.

Defining Your Models

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')});

A Brief Note on Adapters

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.

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 therecord can't be found or there was an error during the request, thepromise will be rejected.

Even More Documentation

For much more detail on how to use Ember Data, see theEmber.js Guideson models.

Building Ember Data

  1. Ensure thatNode.js is installed.
  2. Runnpm install to ensure the required dependencies are installed.
  3. Runnpm run build:production to build Ember Data. The builds will be placed in thedist/ directory.

Contribution

SeeCONTRIBUTING.md

How to Run Unit Tests

Setup

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

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

  3. Runnpm install inside the project root to install the JS dependencies.

In Your Browser

  1. To start the development server, runnpm start.

  2. Visithttp://localhost:4200

From the CLI

  1. Install phantomjs fromhttp://phantomjs.org

  2. Runnpm test

About

A data persistence library for Ember.js.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript99.3%
  • Other0.7%

[8]ページ先頭

©2009-2025 Movatter.jp