问题描述
我正在学习Ember,经过一些基本测试之后,我尝试连接我的API以创建搜索组件。
I'm learning Ember and after some basic test, I tried to connect with my API to created a search component.
API'application / vnd .api + json'
{
"data": [
{
"expediente": "1717801",
"fecha_de_presentacion": "01/02/2016 12:00:00 AM",
"figura_juridica": "REGISTRO DE MARCA",
"logotipo": null,
"signo": "IGUY",
"tipo": "NOMINATIVA",
"titular": "SAMSONITE IP HOLDINGS S.\u00c0.R.L."
},
{
"expediente": "1717793",
"fecha_de_presentacion": "01/02/2016 12:00:00 AM",
"figura_juridica": "REGISTRO DE MARCA",
"logotipo": "1_files/direct_151.gif",
"signo": "URGOSTART",
"tipo": "MIXTA",
"titular": "HCP HEALTHCARE ASIA PTE. LTD"
},
{
"expediente": "1717780",
"fecha_de_presentacion": "02/02/2016 12:00:00 AM",
"figura_juridica": "REGISTRO DE MARCA",
"logotipo": null,
"signo": "SKALAR",
"tipo": "NOMINATIVA",
"titular": "SKALAR HOLDING B.V."
},
{
"expediente": "1717811",
"fecha_de_presentacion": "02/02/2016 12:00:00 AM",
"figura_juridica": "REGISTRO DE MARCA",
"logotipo": "1_files/direct_189.gif",
"signo": "MELI MELO",
"tipo": "MIXTA",
"titular": "MELI\u00b4MELO\u00b4 LIMITED"
}
]
}
应用程序/模板/index.hbs
{{input value=search}}
<ul>
{{#each model as |trademark|}}
<li>{{trademark.signo}}</li>
{{/each}}
</ul>
app / controller / index.js
import Ember from 'ember';
export default Ember.Controller.extend({
queryParams: ['search'],
search: ""
});
app / models / trademarks.js
import DS from 'ember-data';
export default DS.Model.extend({
figura_juridica: DS.attr('string'),
expediente: DS.attr('string'),
titular: DS.attr('string'),
signo: DS.attr('string'),
tipo: DS.attr('string'),
fecha_de_presentacion: DS.attr('date'),
logotipo: DS.attr('string')
});
app / route.js
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType
});
Router.map(function() {
this.route('trademarks');
});
export default Router;
app / adapters / application.js
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
host: 'http://localhost:5000',
namespace: 'v1'
});
如果我从 JSONAPIAdapter 扩展,则出现此错误:
if I extends from JSONAPIAdapter i got this error:
我从 JSONAPISerializer 扩展了我的适配器,但只得到了另一个错误
I changed to extended my adapter from JSONAPISerializer but only got another error.
import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({
host: 'http://localhost:5000',
namespace: 'v1'
});
推荐答案
选项1-使用JSONAPIAdapter
开箱即用Ember.js期望使用JSONApi标准的服务器响应。您可以在此处看到此标准:
当使用 JSONAPIAdapter
时,服务器响应应如下所示:
When you use JSONAPIAdapter
, your server response should look like this:
{
"data": [
{
"type": "trademarks",
"id": "1",
"attributes": {
"expediente": "1717801",
"fecha_de_presentacion": "01/02/2016 12:00:00 AM",
"figura_juridica": "REGISTRO DE MARCA",
"logotipo": null,
"signo": "IGUY",
"tipo": "NOMINATIVA",
"titular": "SAMSONITE IP HOLDINGS S.\u00c0.R.L."
}
},
{
"type": "trademarks",
"id": "2",
"attributes": {
"expediente": "1717793",
"fecha_de_presentacion": "01/02/2016 12:00:00 AM",
"figura_juridica": "REGISTRO DE MARCA",
"logotipo": "1_files/direct_151.gif",
"signo": "URGOSTART",
"tipo": "MIXTA",
"titular": "HCP HEALTHCARE ASIA PTE. LTD"
}
},
.
. rest of the data
.
]
}
您可以使用一些外部包来帮助从烧瓶中生成jsonAPI标准有效负载:,,
You can use a few external package which helps generate jsonAPI standard payload from flask: https://github.com/vertical-knowledge/ripozo/, https://github.com/benediktschmitt/py-jsonapi, https://github.com/xamoom/xamoom-janus
您可以通过重写导出默认DS.JSONAPIAdapter.extend
来更改适配器导出到 /app/adapters/application.js
中的默认DS.RESTAdapter.extend 。
You can change your adapter with rewriting export default DS.JSONAPIAdapter.extend
to export default DS.RESTAdapter.extend
in /app/adapters/application.js
.
在这种情况下,您的服务器有效负载应如下所示:
In this case, your server payload should look like this:
{
"trademarks": [
{
"id": "1"
"expediente": "1717801",
"fecha_de_presentacion": "01/02/2016 12:00:00 AM",
"figura_juridica": "REGISTRO DE MARCA",
"logotipo": null,
"signo": "IGUY",
"tipo": "NOMINATIVA",
"titular": "SAMSONITE IP HOLDINGS S.\u00c0.R.L."
},
{
"id": "2"
"expediente": "1717793",
"fecha_de_presentacion": "01/02/2016 12:00:00 AM",
"figura_juridica": "REGISTRO DE MARCA",
"logotipo": "1_files/direct_151.gif",
"signo": "URGOSTART",
"tipo": "MIXTA",
"titular": "HCP HEALTHCARE ASIA PTE. LTD"
},
.
.
.
]
}
如果您正在学习Ember.js,我写了一篇关于最新的Ember,也许也有帮助:
If you are learning Ember.js, I wrote a free tutorial about the latest Ember, maybe it could help also: Ember.js tutorial
这篇关于emberjs 2连接到api烧瓶-遇到未定义类型的资源对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!