Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.3.0.js"></script>
  
<script src="http://builds.emberjs.com/beta/ember.js"></script>
  
<script src="http://builds.emberjs.com/beta/ember-data.js"></script>
  
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-mockjax/1.5.3/jquery.mockjax.js"></script>
  
<script type="text/x-handlebars" data-template-name="index">
  <ul>
  {{#if length}}
    {{#each}}
      <li>
         {{title}}
          <ul>
            {{#each comments}}
              <li>{{body}}</li>
            {{/each}}
          </ul>
      </li>
    {{/each}}
   {{else}}
     <p>No posts found</p>
   {{/if}}
  </ul>
</script>
  
</body>
</html>
 
App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter;
App.Post = DS.Model.extend({
  title: DS.attr('string'),
  comments: DS.hasMany('comment')
});
App.Comment = DS.Model.extend({
  body: DS.attr('string')
});
// Why does this not work with RESTSerializer?
App.PostSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    comments: {
      embedded: 'always'
    }
  }
});
App.IndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('post');
  }
});
var posts = JSON.stringify({
  posts: [{
    id: 1,
    title: 'Title 1',
    comments: [{
      id: 1,
      body: 'Comment 1'
    }, {
      id: 2,
      body: 'Comment 2'
    }]
  }, {
    id: 2,
    title: 'Title 2',
    comments: [{
      id: 3,
      body: 'Comment 3'
    }]
  }]
});
$.mockjax({
  url: '/posts',
  responseText: posts
});
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers