Chapter 6. Communicating with the API Server Using ember-data

In the last chapter, we learned about the Ember.js controllers. We learned about how to decorate our models with view-specific business logic using Ember.js controllers. In this chapter, we shall improve our understanding of how to effectively communicate with the backend server using ember-data library.

In this chapter, we shall cover:

  • Introducing ember-data
  • Defining ember-data models:
    • Defining relationships between your ember-data models
    • One-to-one
    • One-to-many
    • Many-to-many
  • Understanding the ember-data identity map – DS.Store
  • Working with records:
    • Finding the records
    • Modifying the records
  • The default REST adapter and serializer:
    • Sideloaded relationships
    • Customizing the DS.RESTAdapter
    • Customizing the URL endpoints
  • Example application

Introducing ember-data

Models form the core of any MVC design pattern as they describe the core business domain at hand. In Ember.js, every route has an associated model that describes the business domain that the route needs to work on. Almost all of the examples that we have seen so far operate on the static data that is returned from the model property of the routes. But most of the times the data that needs to be operated upon is not static and is fetched from a remote server.

Traditionally we have used jQuery or plain JavaScript to fetch the data from the server, using AJAX calls and then using those returned JSON objects as models in our application. This approach works well for applications that are simpler in nature, but very quickly get difficult to maintain when working on domains that are complex in nature, especially the ones that have relationships among them. We need to make sure that the correct relationship is linked and updated in our application.

The ember-data library tries to solve the problems highlighted earlier that are encountered in building single page web applications, by providing a standard way of accessing, finding, modifying, and saving the model objects of your application. By using the ember-data library, you can easily simplify your code that handles the communication with the backend server, by letting ember-data handle most of the complexity for you. This lets you focus more on solving the business problem at hand. The library also brings in a lot of optimizations to your code that improve the overall performance of your application.

Ember Data is designed to be agnostic to the underlying storage technique, and hence it works equally well will HTTP based JSON API, as well as streaming WebSockets.

The ember-data library is actively being developed and improved, and is under the beta quality. The API is highly stabilized and there are a lot of companies who have been using ember-data in production for quite some time now.

The current beta of ember-data is already included with Ember CLI and can be checked from the bower.json file, present in the project's root directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.242.235