2

In an application trying to simulate a newsfeed, I have the following three models.

  1. Feed - {name:string, posts:hasmany(Post model)}
  2. Post - {text:string, comments:hasmany(Comment model), feed:belongsto(Feed model)}
  3. Comment - {text:string, post:belongsto(Post model)}

All the three models have separate Rest Apis for retrieving payload. I need to be able to retrieve data and show them under a single route resource.

The RestAdapter documentation at http://emberjs.com/guides/models/the-rest-adapter/ says

Comments for a post can be loaded by post.get('comments'). The REST adapter will send a GET request to /comments?ids[]=1&ids[]=2&ids[]=3.

The code below shows how I am trying to do the same, but I'm unable to retrieve all the data in the Feed model.

var Newsfeed = Ember.Application.create();

Newsfeed.Router.map(function(){
    this.route('feed', {path:'/'});
});

Newsfeed.FeedRoute = Ember.Route.extend({
    model: function(){
        var model = Newsfeed.Feed.find(1);
        //model.set('posts', Newsfeed.Post.find()); -- This also did not work.
        //posts.set('comments', Newsfeed.Comment.find());
        return model;
    },
    setupController: function(controller, model){
        controller.set('content', model);
        controller.set('content.posts', model.get('posts'));
        console.log(controller.get('content.posts'));
        this.controllerFor('posts').set('content', model.get('posts'));
    }
});

Newsfeed.PostsView =  Ember.View.extend({

});
Newsfeed.PostsController = Ember.ArrayController.extend({

});

Newsfeed.FeedController = Ember.Controller.extend({

});
Newsfeed.Store = DS.Store.extend({
    revision:12,
    adapter: 'DS.RESTAdapter'
});

Newsfeed.Feed = DS.Model.extend({
    name:DS.attr('string'),
    posts:DS.hasMany('Newsfeed.Post')
});

Newsfeed.Post = DS.Model.extend({
    text : DS.attr('string'),
    comments:DS.hasMany('Newsfeed.Comment'),
    feed:DS.belongsTo('Newsfeed.Feed')
});

Newsfeed.Comment = DS.Model.extend({
    text: DS.attr('string'),
    post:DS.belongsTo('Newsfeed.Post')
});

I am using jquery.mockjax to simulate rest apis. My payloads are-

{ "feed" :{
id:1,
name:"Ember Web App",
posts :[1,2]
}
}
{"posts" : [{
        id:1,
        text:"orem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempo",
        comment_ids:[1],
        feed_id:1
    },{
        id:2,
        text:"orem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempo",
        comments:[2],
        feed_id:1

    }]
 }

Any pointers on what I might be missing here?

Pavithra Kodmad
  • 255
  • 1
  • 6
  • Are you trying to sideload the "posts"? Have you tried making sure it is working with just the feed first? – RyanJM Mar 14 '13 at 02:48
  • Yes, In my template, I'm able to display the feed[1].name. But When I try iterating through the posts, I dont get any data. I can access the posts data from the PostsController though. – Pavithra Kodmad Mar 14 '13 at 05:25
  • If that is your exact payload it looks like you are missing a comma between feed and posts and have an extra curly. The pay,lad should be a single Json hash with two keys, feed and posts. Maybe that will help? Is your post controller doing its own request? – RyanJM Mar 14 '13 at 12:37
  • No, I have a /feeds api to get the feed payload and /posts api to get the posts payload and another for comments too. If they are in the same payload then that would just be sideloading right? I want to make separate calls and get the data in one route preferably through the 'content' of the feed controller. – Pavithra Kodmad Mar 14 '13 at 17:36
  • 3
    @PavithraKodmad, do you want post back how you got it working. thanks – brg Apr 09 '13 at 10:33

0 Answers0