EmberJS - Models


Advertisements

Model is a class that extends the functionality of the Ember Data. When a user refreshes the page, the contents of page should be represented by a model. In Ember.js, every route has an associated model. The model helps to improve the performance of application. The Ember Data manipulates the stored data in the server and also works easily with streaming APIs like socket.io and Firebase or WebSockets.

Core Concepts

  • Store
  • Models
  • Records
  • Adapter
  • Caching

Store

The store is a central repository and cache of all records available in an application. The route and controllers can access the stored data of your application. The DS.Store is created automatically to share the data among the entire object.

import Ember from 'ember';

export default Ember.Route.extend ({
   model() {
      return this.store.find();
   }
});

Models

Model is a class that extends the functionality of the Ember Data, which specifies relationships with other objects. When a user refreshes the page, the contents of page should be represented by a model.

import DS from 'ember-data';

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

Records

A record is an instance of a model that includes the information, which is loaded from a server and you can identify the record by its model type and ID.

//It finds the record of type 'person' and an 'ID' of 1
this.get('store').findRecord('person', 1); // => { id: 1, name: 'steve-buscemi' }

Adapter

An adapter is an object that is responsible for translating requested records from Ember into appropriate calls to particular server backend. For instance, if you want to find a person with ID of 1, then Ember will load the URL by using HTTP as /person/1.

Caching

The records can be cached automatically by the store and returns the same object instance when you load the records from the server for the second time. This improves the performance of your application and displays the application UI to the user as fast as possible.

The following table lists down the details about models −

S.No. Model Ways & Description
1 Defining Models

Model is a simple class that extends the functionality of the Ember Data.

2 Finding Records

You can retrieve the records by using the Ember data store.

3 Creating and Deleting Records

You can create and delete the records on the instance of model.

4 Relationships

Ember.js provides relationship types to specify how the models are related to each other.

5 Pushing Records Into The Store

You can push the records into the store's cache without requesting the records from an application.

6 Handling Metadata

Metadata is a data that is used for specific model or type instead of using record.

7 Customizing Adapters

Ember.js Adapter specifies how data is kept on at the backend data store such as URL format and REST API headers.

Advertisements