store.find()以relationship-object作为参数?

时间:2015-02-01 13:07:23

标签: ember.js ember-data ember-router

我有两个型号Post.jsCategory.js

Post.js

import DS from 'ember-data';

var Post = DS.Model.extend({
  title: DS.attr('string'),
  permalink: DS.attr('string'),
  body: DS.attr('string'),
  category: DS.belongsTo('category')
});

export default Post;

Category.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  permalink: DS.attr('string'),
  posts: DS.hasMany('post',{async:true})
});

我想通过以下方式检索属于某个类别的所有帖子 http://localhost:4200/<category_permalink>

我无法检索路线中的数据,因为category.permalink因此不起作用。

路线

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {
    return this.store.find('post', { category.permalink: params.category });
  }
});

1 个答案:

答案 0 :(得分:1)

首先,{ category.permalink: params.category }不是有效的JSON。你可能想要实现的是拥有这样一个终点:

GET http://localhost:4200/api/post?category_permalink=:category_permalink`

然后,在Ember,你宁愿使用:

this.store.find('post', {
  category_permalink: params.category
});

并且来自服务器的响应看起来应该或多或少:

{
  "post": [{
    "id": 1,
    "title": "title",
    "body": "body",
    "permalink": "permalink",
    "category": 10
  }],
  "category": {
    "id": 10,
    "name": "name",
    "permalink": "c_permalink",
    "posts": [1,2,3]
  }
}