Hello once again to Readers’ Questions, presented by the folks at The Ember Times with a little help from the Ember Core Team and friends
Today I’ll be answering a question by Darren:
What are the benefits of using Ember Data over POJO/Ajax?
Ember Data is an Ember addon which helps you with creating requests, normalising and serialising responses from and to your backend and efficiently managing a local cache of data. It is an Object Relational Mapping (ORM) for your Ember app and if you’re familiar with e.g. Rails, it might remind you a lot of the way ActiveRecord
works. Ember Data provides another layer of abstraction for the interaction with your backend or third-party APIs.
First, using Ember Data is helpful with refactoring your application in the future, in case that the format of your API response changes substantially, e.g. by switching from an OpenAPI REST format to one defined by the JSON:API spec. Using Ember Data there is no need to rewrite big parts of the application to accommodate the new API format; instead all essential changes are restricted to your app’s adapter and serialiser layer, leaving model definitions and invocations of request functions unchanged.
Second, Ember Data as an abstraction layer enforces a clear separation of concerns between the UI and data layer in your application. This makes your app’s code base easier to manage and scale. Relying on a conventional data library like Ember Data also helps developers of the project with navigating the code base and helps with the on-boarding of new developers on the project, who - already equipped with previous Ember Data experience or after learning more about its conventions - are able to start building new features without requiring project-specific knowledge.
Third, building your own data layer is hard. What might not be obvious at first when starting out building a small, minimal viable product that has to interact with a backend, becomes more apparent, once the application scales and more nuanced data requirements emerge. As you continue building your app, you might run into questions, such as: How do you update the relationships of a resource that you are modifying? How do you ensure that data that has already been fetched from the API is not unnecessarily re-fetched to reduce the amount of requests to the server efficiently? A dedicated data library like Ember Data provides you built-in solutions for many different data management problems in a mid-sized to large application out of the box, including
-
Data caching using the store service. If you’ve fetched a resource from your API, the store will automatically cache the related data for the lifetime of your app and return data directly from there if another part of your application later on requests for the same piece of data. This saves time in rendering your UI and might even save you additional requests to your backend (if you explicitly tell Ember Data to do so). To learn more about data caching in Ember Data, be sure to check out this great example in the Ember Data Guides and read more about its benefits here.
-
Easy access to community-maintained adapter solutions for different API formats. Read also more about the benefits of exchangeable data adapters here.
-
A descriptive API for loading records with and without associated models. Ember Data provides conventions for configuring models and loading exactly these parts of records from your API that you currently need.
-
Out-of-the-box validation error handling through models. If your server returns errors on e.g. saving a new record, those are automatically reflected on your Ember Data records. You can read more about Ember Data’s built-in validation errors here.
Last but not least, it is important to mention that even though Ember Data is installed into each Ember app that is newly created using Ember CLI by default, it is not mandatory to use Ember Data in your Ember app. Besides writing your own custom data management tools tailored for your application, you may also benefit from the community’s efforts to make Ember interoperable with all kinds of APIs and data layers. Ember Apollo Client and Ember Redux are just two examples of excellent, popular and well-maintained data library alternatives to Ember Data. You can find many other top community addons on Ember Observer.
Wanna learn more about Ember Data? Be sure to read the improved, official Ember Data Guides, to check out this stellar intro into building a CRUD app using Ember Data and to join the discussions on the Ember Data Discord channel.
This answer was published in Issue #87 of The Ember Times. Many thanks go to both @CodingItWrong and @runspired for reviewing this week’s Readers’ Question✨
Last but not least, don’t forget to subscribe to the newsletter to get new Readers’ Questions and other development news right to your inbox. You can also submit your own questions! You will find a link at the bottom of the newsletter.
See you in the next issue!