[英]How to use Ember Data serializer wth JQuery AJAX requests?
I've been using Ember-Data very well where my API endpoints are like我一直在很好地使用 Ember-Data,我的 API 端点就像
domain.com/apples/
=> list of apples domain.com/apples/
=> 苹果列表
domain.com/apples/1/
=> Apple with id 1 domain.com/apples/1/
=> id 为 1 的苹果
domain.com/worms/
=> list of worms domain.com/worms/
=> 蠕虫列表
I need to fetch all the worms in one apple.我需要在一个苹果中取出所有的蠕虫。 My API endpoint for this is like
我的 API 端点就像
domain.com/apples/1/worms/
=> list of worms in apple with id 1 domain.com/apples/1/worms/
=> 苹果中 id 为 1 的蠕虫列表
I've been fetching those worms with Em.$.getJSON(...)
and works fine.我一直在用
Em.$.getJSON(...)
获取那些蠕虫并且工作正常。 But the result is a list of Object and I want a list of Worm ( defined in /models/worms.js )但结果是一个 Object 列表,我想要一个 Worm 列表(在 /models/worms.js 中定义)
Have a look at ember-api-actions .看看ember-api-actions 。
Your model will then look like this:您的模型将如下所示:
// app/models/apple.js
import DS from 'ember-data';
import { memberAction } from 'ember-api-actions';
export default DS.Model.extend({
// GET /apples/:id/worms
worms: memberAction({ path: 'worms', type: 'get' })
});
To fetch all worms for a given apple, use something like this:要获取给定苹果的所有蠕虫,请使用以下内容:
this.store.findRecord('apple', params.id).then((apple) => apple.worms())
PS It's also possible to manually push the JSON response to the ember data store but implementing a custom adapter is more robust (something which you can see is made ridiculously easy by ember-data-actions) PS 也可以手动将 JSON 响应推送到 ember 数据存储,但实现自定义适配器更加健壮(您可以看到,ember-data-actions 使您可以轻松实现这一点)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.