A assignment can have many submissions. I'm trying to get the submissions to display inside the assignment template, using the /assignments/1 route. (Displaying data from assignment is working as expected.) I'm using Ember 2.10 and Ember data 2.10.
app/router.js:
this.route('assignments', function() {
this.route('assignment', {path: ':assignment_id'});
});
app/routes/assignments/assignment.js:
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return Ember.RSVP.hash({
assignments: this.store.findRecord('assignment', params.assignment_id),
submissions: this.store.findAll('submission', params.assignment_id)
});
},
setupController: function(controller, models){
controller.set('assignment', models);
controller.set('submission', models);
}
});
app/models/assignment.js:
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
ispublic: DS.attr('boolean'),
emailnotification: DS.attr('boolean'),
markdown: DS.attr('string'),
submission: DS.hasMany('submission')
});
app/models/submission.js:
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
markdown: DS.attr('string'),
assignment: DS.belongsTo('assignment')
});
app/templates/assignments/assignment.hbs:
{{assignment.title}}
{{#each submissions as |submission|}}
{{submission.title}}
{{/each}}
I'm using the http-mock from ember-data with the JSONAPI format. The server doesn't do an extra request the get the all the submissions that are related to an assignment. I'm not sure if it has to do an extra request:
server/mocks/assignments.js:
module.exports = function(app) {
var express = require('express');
var assignmentsRouter = express.Router();
var assignments = {
"data": [
{
"type": "assignments",
"id": "1",
"attributes": {
"title": "Deze is niet public",
"ispublic": "false",
"emailnotification": "false",
"markdown": "This is the markdown of the first assignment"
},
"relationships": {
"submissions": {
"data": [
{ "type": "submissions", "id": "1" }
]
}
}
},
{
"type": "assignments",
"id": "2",
"attributes": {
"title": "Deze is public",
"ispublic": "true",
"emailnotification": "false",
"markdown": "This is the markdown of the second assignment"
},
"relationships": {
"submissions": {
"data": [
{ "type": "submissions", "id": "2" },
{ "type": "submissions", "id": "3" }
]
}
}
}]
};
assignmentsRouter.get('/:id', function(req, res) {
//No need for this apparantly ember data caches the findAll request which gets all the assignments...
});
app.use('/api/assignments', assignmentsRouter);
};
server/mocks/submissions.js:
module.exports = function(app) {
var express = require('express');
var submissionsRouter = express.Router();
var submissions = {
"data": [
{
"type": "submissions",
"id": "1",
"attributes": {
"username": "Myusermane",
"title": "Pretty title",
"markdown": "Some text"
},
"relationships": {
"assignments": {
"data": [
{ "type": "assignments", "id": "1" }
]
}
}
},
{
"type": "submissions",
"id": "2",
"attributes": {
"username": "Somebody",
"title": "Nice title",
"markdown": "Some other text"
},
"relationships": {
"assignments": {
"data": [
{ "type": "assignments", "id": "2" }
]
}
}
},
{
"type": "submissions",
"id": "3",
"attributes": {
"username": "Whoelse",
"title": "Test",
"markdown": "Text"
},
"relationships": {
"assignments": {
"data": [
{ "type": "assignments", "id": "2" }
]
}
}
}]
};
submissionsRouter.get('/', function(req, res) {
res.send(submissions);
});
app.use('/api/submissions', submissionsRouter);
};
Aucun commentaire:
Enregistrer un commentaire