When the application initializes, Ember Data creates store
, a local store
object.
this.store
is the object that will create,
retrieve, update, and delete all of the Tracker app’s model records.
Ember injects the store
object in all Routes
,
Controllers
, and Components
. In the
scope of route methods, you have access to the store
from
this
.
To create a record, you will call this.store.createRecord. This method expects two arguments: a model name, as a string, and record data, as an object.
Open app/routes/sightings.js. Delete your dummy sightings and create three new sighting records, each with a location value as a string and a sightedAt value as a new Date:
import Ember from 'ember'; export default Ember.Route.extend({ model() {return [{id: 1,location: 'Asilomar',sighted_at: new Date('2016-03-07')},...{id: 6,location: 'Asilomar',sightedAt: new Date('2016-03-07')}];let record1 = this.store.createRecord('sighting', { location: 'Atlanta', sightedAt: new Date('2016-02-09') }); let record2 = this.store.createRecord('sighting', { location: 'Calloway', sightedAt: new Date('2016-03-14') }); let record3 = this.store.createRecord('sighting', { location: 'Asilomar', sightedAt: new Date('2016-03-21') }); return [record1, record2, record3]; } });
In Chapter 20, the sightings
route
model returned an array. Instead of returning JavaScript objects,
you have created three sighting records and returned these records in
an array. Run ember server
, if it is not running
already, to see your new records on the
sightings
route,
http://localhost:4200/sightings
(Figure 21.1).
This example shows that creating Ember Data models is very similar to creating JavaScript objects. The advantage to having Ember Data model objects is all the methods these objects give you. Let’s start with get and set.
18.116.50.87