lundi 23 mars 2015

ember.js, How do I load related models while loading main model from server that is not support sideloading

I'm ember.js newbie and facing daily problems :-)


Today's problem is my server backend doesn't support sideloading. (If I understood 'sideload' right, it is server returns related models at once with multiple JSON root)


for example, my server only returns below per request:


for /api/v1/posts.json



{
posts: [{
id: 91,
title: "ember.js"
}, {
id: 81,
title: "ember-data.js"
}]
}


for /api/v1/comments.json



{
comments: [{
id: 928,
postId: 91,
}, {
id: 927,
postId: 92,
}]
}


When I load post model, go /#/posts, the view didnot render comment but if I load comment manually by typing URL of comment route in location bar, /#/comments, and go back, the comments are displayed properly.


so I just try like this, in post route, (third line)



App.PostsRoute = Ember.Route.extend({
model: function(params) {
this.store.find('comment');
return this.store.find('post');
}
})


and it works! (load and populate comment too!) but I think it is not right way.


Is there any good or RIGHT way to do this?





Aucun commentaire:

Enregistrer un commentaire