Ember developers have great options for how they handle data from back end APIs. Ember itself works with any type of back end: REST, JSON:API, GraphQL, or anything else. This guide will summarize how and where to make API requests. Follow the links within it to see examples and learn more.
Some common tools for making CRUD (create, read, update, delete) requests in Ember include:
fetch. Install ember-fetch in order to provide support for older browsers, and import fetch from 'fetch' to use it.import jQuery from 'jquery' in your app.API requests can be made almost anywhere in an Ember app, however the most common place is the model hook of a Route.
model hookIn almost every case, this is where your app should load data. You can see examples and more information in Specifying a Route's Model.
These are the main reasons to load data in a model hook:
Some people choose to load data in their Components. The drawback is that requires more work from developers to handle async, rendering, errors, concurrency, and URL state themselves - functionality they would get automatically if they used a Route's model hook. However there are valid use cases for loading data in a component, for developers who are comfortable handling the router's features themselves.
Some common use cases include:
These Guides do not cover how to load data in components, since the majority of data fetching should be done in a route's model.
If someone is connecting to a third-party API, such as a service for payment or mapping, and they need that state across many routes, a Service might be a good place to make requests. Some common use cases include polling for data and managing websocket connections.
Requests in services have the same drawbacks as Components. Functions and state in a Service can be used almost anywhere in the app.
You don't need to build a back end in order to see how your app might work once it has real data loading in! Check out the official Ember.js Tutorials to learn how to simulate API requests in an app and test your data loading.
Here are some top things to know if you are new to making API requests in a front end framework:
© 2022 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://guides.emberjs.com/v4.9.0/in-depth-topics/making-api-requests