我试图了解使用ember-model侧载数据的正确方法
我有 json 像这样回来(为了这里的空间,我把它从实际的 json 中缩小了一点)
{
"classrooms" : [
{
"classroom_name" : "Class 1",
"id" : 1,
"teacher_id" : 3,
"grade" : 5,
"assignments" : [
],
"students" : [
{
"id" : 5,
"last_name" : "Ford",
"first_name" : "Henry",
"district_id_number" : "MD454"
}
]
},
{
"classroom_name" : "Class 3",
"id" : 2,
"teacher_id" : 3,
"grade" : 4,
"assignments" : [
{
"id" : 5,
"assignment_overview" : "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.\r\n\r\nNam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum.",
"assignment_title" : "Fractions",
"story" : null
}
],
"students" : [
{
"id" : 5,
"first_name" : "Henry",
"last_name" : "Ford",
"district_id_number" : "MD454"
},
{
"id" : 3,
"first_name" : "Jake",
"last_name" : "Strong",
"district_id_number" : "WH6879"
},
{
"id" : 6,
"first_name" : "Bryan",
"last_name" : "Dobson",
"district_id_number" : "B453"
}
]
}
]
}
在我的课堂模型中,我有一个计算属性,就像这样,我循环嵌入的学生对象,将它们加载到侧载数据中,然后使用 find 将它们拉出来。
classroomStudents: function () {
var studentObjects = [],
students = this.get('students');
Msmapp.Student.load(students);
students.forEach(function(student) {
studentObjects.pushObject(Msmapp.Student.find(student.id));
});
return studentObjects;
}.property('students')
我认为这this.get('students')
可能不是Msmapp.Student.load(students);
预期的。我假设它需要原始格式的数据,而我不是 100% 肯定this.get('students')
的。
这就是this.get('students')
我调试的时候
[Object
resource_document_ids: Array[0]
resource_ids: Array[0]
resource_image_ids: Array[0]
resource_video_ids: Array[0]
__proto__: Object
district_id_number: "MD454"
first_name: "Henry"
id: 5
resource_document_ids: Array[0]
resource_ids: Array[0]
resource_image_ids: Array[0]
resource_video_ids: Array[0]
__proto__: Object
,Object
,Object
]
当我调试返回的studentObjects
数组时,我得到了类,但它们似乎不正确
[Class
__ember1372909895769: undefined
__ember1372909895769_meta: Meta
_super: undefined
data: Object
isLoaded: true
isNew: false
__proto__: Object
id: 5
resource_document_ids: Array[0]
resource_ids: Array[0]
resource_image_ids: Array[0]
resource_video_ids: Array[0]
__proto__: Object
__defineGetter__: function __defineGetter__() { [native code] }
__defineSetter__: function __defineSetter__() { [native code] }
__lookupGetter__: function __lookupGetter__() { [native code] }
__lookupSetter__: function __lookupSetter__() { [native code] }
constructor: function Object() { [native code] }
hasOwnProperty: function hasOwnProperty() { [native code] }
isPrototypeOf: function isPrototypeOf() { [native code] }
propertyIsEnumerable: function propertyIsEnumerable() { [native code] }
toLocaleString: function toLocaleString() { [native code] }
toString: function toString() { [native code] }
valueOf: function valueOf() { [native code] }
isLoaded: true
isNew: false
, Class
, Class
]
在我的模板中,我有这样的东西
<ul>
{{#if classroomStudents }}
{{#each student in classroomStudents }}
<li class="listed_item micro">
{{#linkTo "classroom_student" student }}
<div class='title'>{{ student.first_name }}</div>
{{/linkTo}}
</li>
{{/each}}
{{ else }}
<li class="item">
{{#linkTo "classroom.new_student" classNames='header_link tooltip'}}
No students assigned
{{/linkTo}}
</li>
{{/if}}
</ul>
我没有得到任何值,因为它们似乎没有在对象上设置,但链接工作正常。我想这是因为正在设置 id。
两者{{ student.first_name }}
或{{ first_name }}
都未定义。