在ember夹具适配器中做关系吗?

时间:2014-04-22 23:34:56

标签: javascript ember.js

不确定我在这里做错了什么,只是尝试使用夹具适配器获得使用ember数据的一对多关系。这是jsbin:http://jsbin.com/ucanam/4716/edit?html,js,output

我在{{each}}循环的html中看不到任何输出。

JS:

App = Ember.Application.create({});

App.ApplicationAdapter = DS.FixtureAdapter.extend();

App.Post = DS.Model.extend({
  body: DS.attr('string'),
  topic: DS.belongsTo('topic')
});

App.Topic = DS.Model.extend({
  posts: DS.hasMany('post')
});

App.Topic.FIXTURES = [
  {
    id: 1,
    posts: [
      {
        id: 1,
        body: 'hey i am a post',
        topic_id: 1
      }
    ]
  }
];

App.Router.map(function() {
  this.resource('topics', { path: '/' });
});

HTML:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
  <script src="http://code.jquery.com/jquery-2.0.2.js"></script>
  <script src="http://builds.emberjs.com/handlebars-1.0.0.js"></script>
  <script src="http://builds.emberjs.com/ember-latest.js"></script>
    <script src="http://builds.emberjs.com/ember-data-latest.js"></script>

</head>
<body>
  <script type="text/x-handlebars" data-template-name="application">
    <h1>ember-latest jsbin</h1>
    {{outlet}}
  </script>
  <script type="text/x-handlebars" data-template-name="topics">
    <h2>Index Content:</h2>
    <ul>
      {{#each topic in topics}}
        <li>{{id}}</li>
        {{#each post in posts}}
          {{body}}
        {{/each}}
      {{/each}}
    </ul>
  </script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

Fixture Adapter默认情况下不支持嵌入式关系。实现此目的的最简单方法是将您的关系设置为异步并将其添加到帖子的灯具中。

App.Post = DS.Model.extend({
  body: DS.attr('string'),
  topic: DS.belongsTo('topic', {async: true})
});

App.Topic = DS.Model.extend({
  posts: DS.hasMany('post', {async: true})
});

App.Topic.FIXTURES = [
  {
    id: 1,
    posts: [1]
  }
];

App.Post.FIXTURES = [
  {
    id: 1,
    body: 'hey i am a post',
    topic: 1
  }
];

这是你的jsbin设置,http://jsbin.com/ucanam/4718/edit