0

我只想刷新路由中的模型,同时从控制器获取操作并在此路由中运行 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'));

但控制台日志说未定义

你能解决这个问题并解释在我的这条路线上发生了什么..

感谢你的关心

4

1 回答 1

1

您的问题是您无法直接在动作处理程序中实现从路由内返回的对象,例如this.get('profileFormsAttributes');因此您的设置不起作用。

this.get('controller.model.profileFormsAttributes');
this.get('model.profileFormsAttributes');

即使以上两个语句也不起作用;因为你无法检索modelcontroller喜欢这个。

你有两个选择;要么您需要将要从模型中直接返回的内容保存在model钩子中,this.set('model', model)要么您可以使用this.controllerFor(this.get('routeName')).get('model')

我会为您的情况推荐第二种方法。请看下面我准备的为你说明情况的小玩意儿。

请查看从钩子返回的对象的属性在index.js哪里设置foomodel

Ember.set(this.controllerFor(this.get('routeName')).get('model'), 'foo', 'foo updated');

我希望这有帮助。

于 2017-04-11T16:11:18.963 回答