问题描述
我有一个有很多学生的示范老师。模型定义如下: App.Teacher = DS.Model.extend({
email:DS。 attr('string'),
学生:DS.hasMany('student')
});
App.Student = DS.Model.extend({
teacher:DS.belongsTo('teacher'),
});
当教师登录时,服务器返回教师的JSON表示形式:
{
id:1,
email:abc@example.com,
links:{
学生:/老师/ 1 /学生
}
}
在登录控制器中,然后我将这些数据推入商店并将其存储在会话控制器的财产中:
this .set('currentUser',this.get('store')。push('teacher',teacherJson))
我想懒加载学生
关联,所以我使用API中定义的链接格式()。所以,理想情况下,每当我调用
App.SessionController.get('currentUser')。get('students')
它会加载相关的学生
GET请求到 / teacher / 1 / students
。但是从来没有发生过。为什么请求不被触发?
好的,我找到了答案。我必须在教师的模型中添加一个属性
: async:true
到学生
关联
App.Teacher = DS.Model.extend({
email:DS .attr('string'),
学生:DS.hasMany('student',{async:true})
});
I have a model Teacher which has many Students. The models are defined as follows:
App.Teacher = DS.Model.extend({
email: DS.attr('string'),
students: DS.hasMany('student')
});
App.Student = DS.Model.extend({
teacher: DS.belongsTo('teacher'),
});
When a Teacher logs in, the server returns a JSON representation of the Teacher:
{
id: 1,
email: "abc@example.com",
links: {
students: /teacher/1/students
}
}
In the controller for login, I then push this data into the store and store it in a property of the Session controller:
this.set('currentUser', this.get('store').push('teacher', teacherJson))
I want to lazy-load the students
association so I used the "links" format as defined in the API (http://emberjs.com/api/data/classes/DS.Store.html#method_push). So, ideally, whenever I would call
App.SessionController.get('currentUser').get('students')
it would load the associated students
by sending a GET request to /teacher/1/students
. But that never happens. Why is the request not triggered?
Ok, I found the answer. I had to add a property async: true
to the students
association in the model for Teacher
:
App.Teacher = DS.Model.extend({
email: DS.attr('string'),
students: DS.hasMany('student', { async: true })
});
这篇关于具有“链接”的Ember数据延迟加载关联属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!