home
  • Blog
2.4
  • Getting Started
  • Tutorial
    • Creating Your App
    • Setting Up Tests
    • Routes and Templates
    • The Model Hook
    • Installing Addons
    • Using Ember Data
    • Building a Simple Component
    • Creating a Handlebars Helper
    • Building a Complex Component
    • Services and Utilities
    • Deploying
  • The Object Model
  • Routing
  • Templates
  • Components
  • Controllers
  • Models
  • Application Concerns
  • Testing
  • Ember Inspector
  • Addons and Dependencies
  • Configuring Ember.js
  • Contributing to Ember.js
  • Glossary
Old Guides - You are viewing the guides for Ember v2.4.0.
Go to v5.0.0

Using Ember Data

Edit pencil

Currently, our app is using hard-coded data for rentals in the index route handler to set the model. As our application grows, we will want to be able to create new rentals, make updates to them, delete them, and save these changes to a backend server. Ember integrates with a data management library called Ember Data to help solve this problem.

Let's generate our first Ember Data model called rental:

ember g model rental

This results in the creation of a model file and a test file:

installing model
  create app/models/rental.js
installing model-test
  create tests/unit/models/rental-test.js

When we open the model file, we see:

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

export default DS.Model.extend({

});

Let's add the same attributes for our rental that we used in our hard-coded array of JavaScript objects - title, owner, city, type, image, and bedrooms:

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

export default DS.Model.extend({
  title: DS.attr(),
  owner: DS.attr(),
  city: DS.attr(),
  type: DS.attr(),
  image: DS.attr(),
  bedrooms: DS.attr()
});

Now we have a model in our Ember Data store.

Updating the Model Hook

To use our new data store, we need to update the model hook in our route handler.

app/routes/index.js
import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    return this.store.findAll('rental');
  }
});

When we call this.store.findAll('rental'), Ember Data will make a GET request to /rentals. You can read more about Ember Data in the Models section.

Since we're using Mirage in our development environment, Mirage will return the data we've provided. When we deploy our app to a production server, we will need to provide a backend for Ember Data to communicate with.

left arrow
Installing Addons
Building a Simple Component
right arrow
On this page

  • Updating the Model Hook
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue