我只想刷新 route 的模型,同时从 Controller 获取操作并在此 route 运行 doRefresh 操作
这是我的代码
import Ember from 'ember';
export default Ember.Route.extend({
profileFormService: Ember.inject.service(),
profileFormAtributeService: Ember.inject.service(),
model(){
return Ember.RSVP.hash({
profileForms: this.get('profileFormService').find(),
profileFormsAttributes: this.get('profileFormAtributeService').findByProfileFormId("1"),
inputTypes: {data: ['CHECKBOX', 'NUMBER_FIELD', 'PHONE_NUMBER', 'TEXT_FIELD', 'EMAIL', 'TEXT_AREA', 'RADIO_BUTTON', 'DESA', 'KABUPATEN_KOTA', 'PROVINSI', 'KECAMATAN', 'MAP_POINT', 'MAP_POLYGON']}
});
},
setupController(controller, model) {
this.controllerFor('backend.setting-profile-form-attribute').set('profileForms', model.profileForms);
this.controllerFor('backend.setting-profile-form-attribute').set('profileFormsAttributes', model.profileFormsAttributes);
this.controllerFor('backend.setting-profile-form-attribute').set('inputTypes', model.inputTypes);
},
actions:{
doRefresh(param){
let context = this;
this.get('profileFormAtributeService').findByProfileFormId(param).then(function (response) {
context.set("profileFormsAttributes",response);
}), function (e) {
this.debug(e);
};
}
}
});
不幸的是,这不会影响 profileFormsAttributes 模型。
我一直在尝试用这个来调试模型
this.debug(this.get('controller.model.profileFormsAttributes'))
this.debug(this.get('model.profileFormsAttributes'));
但控制台日志显示未定义
你能解决这个问题并解释一下我的 route 发生了什么吗?
谢谢您的关心
最佳答案
您的问题是您无法像 this.get('profileFormsAttributes')
一样直接在操作处理程序中实现从路由内返回的对象;因此你的设置不起作用。
this.get('controller.model.profileFormsAttributes');
this.get('model.profileFormsAttributes');
以上两条语句也不起作用;因为您无法像这样检索 model
或 controller
。
你有两个选择;您需要使用 this.set('model', model)
直接在 model
钩子(Hook)中保存要从模型返回的内容,或者您可以使用 实现它>this.controllerFor(this.get('routeName')).get('model')
对于您的情况,我建议采用第二种方法。请看下面twiddle我已经准备好为您说明这个案例。
请查看 index.js
,其中从 model
Hook 返回的对象的 foo
属性设置为
Ember.set(this.controllerFor(this.get('routeName')).get('model'), 'foo', 'foo updated');
我希望这会有所帮助。
关于javascript - 无法从 ember 中的路线操作获取模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43350608/