作业可以有很多提交。我尝试使用/ assignments / 1路由将提交内容显示在分配模板中。 (显示分配数据正在按预期工作。) 我使用的是Ember 2.10和Ember数据2.10。
应用程序/ router.js:
this.route('assignments', function() {
this.route('assignment', {path: ':assignment_id'});
});
应用程序/路由/任务/ 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);
}
});
应用程序/模型/ 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')
});
应用程序/模型/ submission.js:
import DS from 'ember-data';
export default DS.Model.extend({
title: DS.attr('string'),
markdown: DS.attr('string'),
assignment: DS.belongsTo('assignment')
});
应用程序/模板/任务/ assignment.hbs:
{{assignment.title}}
{{#each submissions as |submission|}}
{{submission.title}}
{{/each}}
我使用带有JSONAPI格式的ember-data的http-mock。服务器不会执行额外请求以获取与分配相关的所有提交。我不确定是否需要额外请求:
服务器/嘲笑/ 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);
};
服务器/嘲笑/ 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);
};