Reputation: 2956
I'm having an issue relating models in Ember.js in regards with retrieving data from restful server (which is served by Sails.js using MongoDB)
My router setup is as follows:
App.Router.map(function() {
this.route("dashboard");
this.resource('exams', {path: "/exams"}, function() {
this.resource('exam', {path: ":exam_id"}, function(){
this.resource('questions', function() {
this.route("new");
})
})
this.route("view", {path: "/:exam_id" }); // for viewing exam details
});
});
Basically I'm getting a list of available examination and is able either edit some details for each, of click on it to see associated list of questions.
I have no problems with the Exam resource which is accessed with restful adapter like so:
http://localhost:1337/api/v1/exams
which yields:
{
exams: [
{
user_id: "52444c03268a31ea0b000001",
title: "Introduction test",
private: false,
active: false,
showResults: false,
random: false,
_id: "52471342445565e74600000a"
},
...
]
}
Questions resource is not embedded and is stored is a separate collection in MongoDB and is accessed separately:
http://localhost:1337/api/v1/questions
with result:
{
questions: [
{
_id: "52483f404617e6c728c4ed93",
title: "What's the capital of Moscow?",
exam_id: "52471342445565e74600000a"
},
{
_id: "52483f6e4617e6c728c4ed94",
title: "What's the capital of Switzerland?",
exam_id: "52471342445565e74600120a"
}
]
}
However, questions should always be related to exams. As far as I understand you cannot nest rest routes in Ember.js yet. My ideal rest route would be:
http://localhost:1337/api/v1/exams/52471342445565e74600000a/questions
to get all the questions for a particular exam, but I'm not sure if this can be done. At least I never manager to get it to work.
So to keep to keep it simple I decided to restfully query questions with exam_id to get only the list of questions associated with particular exam:
http://localhost:1337/api/v1/questions/52471342445565e74600000a //<-- exam_id
which return the results I need... only this is not allowed by Ember.js as it thinks I'm getting a single question w/ particular ID. I also tried looking into pushing exam_id to question restful route w/ URL parameter (../?exam_id=52471342445565e74600000a) but it seems queryParams aren't a part of Ember.js yet. At least non before v 1.2 as I see on Github.
So my question is: How do I related and query these two models by proving a foreign key? I really don't want to embed everything in a single thing. OR maybe there's a better way to manager relations between models?
One caveat though: I need to be able to query server correctly to retrieve only the needed records and not sort through whole prefetched data. This is because different exams belong to different users and question to different exams are not allowed to be seen (queried) by the others.
I'm kinda new to Ember.js so any suggestions (or better yet real world examples) are appreciated.
Upvotes: 2
Views: 959
Reputation: 47367
If you use Ember Data most of this is very simple.
The query string portion is simple:
this.get('store').find('question', {exam_id: 123213});
would produce a GET something like this '/api/questions?exam_id=123213'
The relationship portion is awesome and super easy as well:
App.BlogPost = DS.Model.extend({
comments: DS.hasMany("comment")
});
App.Comment = DS.Model.extend({
post: DS.belongsTo("blogPost")
});
Upvotes: 1