我主要是 Ember 的新手。 我想访问 Controller 中的模型数据。 我正在使用 Ember.RSVP.hash 在我的 route 加载多个模型。
我的路线
model(params) {
this.set('id', params.userID);
return Ember.RSVP.hash({
dashboards: this.store.findAll('dashboard', params)
.then((dashboards) => dashboards.toArray()),
user: this.store.find('user', params.userID)
})
},
setupController(controller, model) {
controller.setProperties(model);
},
用户模型
export default DS.Model.extend({
userID: DS.attr('string'),
email: DS.attr('string'),
name: DS.attr('string'),
picture: DS.attr('string'),
active: DS.attr('boolean'),
permissions: DS.attr(),
created: DS.attr('utc'),
password: DS.attr('string')
});
然后我尝试访问用户模型参数之一。在我的 Controller 中,我创建了 init 函数,并尝试访问用户模型参数。我写的:
init() {
console.log(this.get('user.name'));
},
之后我得到了未定义。 这样做的正确方法是什么?
最佳答案
在 Controller 上使用 this.get('model')
来访问整个模型数据。您还可以使用Ember.get(this, 'model.user.name');
。您可以使用类似以下内容:查看 the actions .
但是 init
是一个急切的钩子(Hook)! model
属性尚未在 init
Hook 处设置!
关于ember.js - 如何访问 Ember Controller 中的模型数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43841818/