如何在不将数据保存在数据库中且不使用 Ember-Data 的情况下将数据从 Rails 后端显示到 Ember 前端?
How do I display data from Rails backend to Ember frontend without saving it in the database and without using Ember-Data?
问题:
我已经使用 rest-client 通过我的 Rails 后端从 newsapi.org 中提取新闻文章。我可以在 localhost:3000/articles 上显示该数据。我(不一定)想将此数据保存到数据库中,并希望在我的 Rails 后端调用它之后简单地在我的 Ember 前端显示它。我收到此错误:
Error while processing route: article Assertion Failed: You must include an 'id' for article in an object passed to 'push'
我理解这意味着我的数据没有 'id' - 它没有。我无法控制传入的数据。
{
"status": "ok",
"totalResults": 6868,
"articles": [
{
"source": {
"id": "mashable",
"name": "Mashable"
},
"author": "Lacey Smith",
"title": "Cannabis may alter the genetic makeup of sperm",
"description": "A new study suggests that marijuana use can not
only lower sperm count, but also affect sperm DNA. Read more... More
about Marijuana, Cannabis, Sperm, Science, and Health",
"url": "https://mashable.com/video/cannabis-sperm-dna/",
"content": null
},
Ember-数据不喜欢这样,因为它不是JSONAPI格式,我没法改成JSONAPI。如何在不使用 Ember-Data 且无需将其保存到我的数据库中的情况下在我的 ember 前端显示此数据?我发现的教程都有使用数据库和 Ember-Data.
的示例
我已经尝试过的:
我试图将它保存到我的数据库并从那里加载,但我认为这可能会使这个想法复杂化,因为我需要抓取数据并将其保存到我的数据库,而事实证明这很难做到.我对后端的东西没有经验,也没有找到很多答案。如果您有这样的答案,请随时在这里回答:
How to automatically save data from url to database
我愿意尝试一下。我原以为这是最好的方法。我可以保存数据,只在返回新文章时才添加到数据库中。我没有这样的运气,所以我想我会从另一个方向接近它。
感谢您的帮助!
如果您想在 ember-data
之外进行一些处理,请创建一个 service. In your service, you will need to then make an ajax/fetch request to your backend. Let's say you are using ember-ajax 来发出您的请求:
import Service, { inject } from '@ember/service';
export default Service.extend({
// inject the ember-ajax ajax service into your service
ajax: inject(),
//this returns a promise
getNewsItems(){
var options = {
// request options (aka anything needed to authenticate against your api, etc)
};
let url = "yourBackendEndpoint/for/this/resource";
return request(url, options).then(response => {
// `response` is the data from the server
// here you want to convert the JSON into something useable
return response;
});
}
});
然后,假设在某个路由的模型挂钩中,您需要此服务(我们将在 your-app/services/my-service.js
中定义)来获取新闻提要项目:
import Route from '@ember/route';
import { inject } from '@ember/service';
export default Route.extend({
myService: inject(),
model(){
let myService = this.get('myService');
return myService.getNewsItems();
}
)
我个人使用 ember-fetch,它避免了 jQuery 依赖性,并且可以在我的应用程序的所有目标浏览器中使用。我不在我的任何应用程序中使用 ember-data。
问题:
我已经使用 rest-client 通过我的 Rails 后端从 newsapi.org 中提取新闻文章。我可以在 localhost:3000/articles 上显示该数据。我(不一定)想将此数据保存到数据库中,并希望在我的 Rails 后端调用它之后简单地在我的 Ember 前端显示它。我收到此错误:
Error while processing route: article Assertion Failed: You must include an 'id' for article in an object passed to 'push'
我理解这意味着我的数据没有 'id' - 它没有。我无法控制传入的数据。
{
"status": "ok",
"totalResults": 6868,
"articles": [
{
"source": {
"id": "mashable",
"name": "Mashable"
},
"author": "Lacey Smith",
"title": "Cannabis may alter the genetic makeup of sperm",
"description": "A new study suggests that marijuana use can not
only lower sperm count, but also affect sperm DNA. Read more... More
about Marijuana, Cannabis, Sperm, Science, and Health",
"url": "https://mashable.com/video/cannabis-sperm-dna/",
"content": null
},
Ember-数据不喜欢这样,因为它不是JSONAPI格式,我没法改成JSONAPI。如何在不使用 Ember-Data 且无需将其保存到我的数据库中的情况下在我的 ember 前端显示此数据?我发现的教程都有使用数据库和 Ember-Data.
的示例我已经尝试过的:
我试图将它保存到我的数据库并从那里加载,但我认为这可能会使这个想法复杂化,因为我需要抓取数据并将其保存到我的数据库,而事实证明这很难做到.我对后端的东西没有经验,也没有找到很多答案。如果您有这样的答案,请随时在这里回答:
How to automatically save data from url to database
我愿意尝试一下。我原以为这是最好的方法。我可以保存数据,只在返回新文章时才添加到数据库中。我没有这样的运气,所以我想我会从另一个方向接近它。
感谢您的帮助!
如果您想在 ember-data
之外进行一些处理,请创建一个 service. In your service, you will need to then make an ajax/fetch request to your backend. Let's say you are using ember-ajax 来发出您的请求:
import Service, { inject } from '@ember/service';
export default Service.extend({
// inject the ember-ajax ajax service into your service
ajax: inject(),
//this returns a promise
getNewsItems(){
var options = {
// request options (aka anything needed to authenticate against your api, etc)
};
let url = "yourBackendEndpoint/for/this/resource";
return request(url, options).then(response => {
// `response` is the data from the server
// here you want to convert the JSON into something useable
return response;
});
}
});
然后,假设在某个路由的模型挂钩中,您需要此服务(我们将在 your-app/services/my-service.js
中定义)来获取新闻提要项目:
import Route from '@ember/route';
import { inject } from '@ember/service';
export default Route.extend({
myService: inject(),
model(){
let myService = this.get('myService');
return myService.getNewsItems();
}
)
我个人使用 ember-fetch,它避免了 jQuery 依赖性,并且可以在我的应用程序的所有目标浏览器中使用。我不在我的任何应用程序中使用 ember-data。