简体   繁体   English

无法从余烬中的路线动作获取模型

[英]cant get model from route action in ember

i just wanna refresh model in route while get an action from controller and run doRefresh action in this route 我只是想在路由中刷新模型,同时从控制器获取操作并在此路由中运行doRefresh操作

this is my code 这是我的代码

    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);
      };
    }
  }
});

unfortunately this is does'nt affect the profileFormsAttributes model. 不幸的是,这不会影响profileFormsAttributes模型。

I've ben trying to debug the model with this 我一直试图以此调试模型

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

but the console log said undefined 但是控制台日志说未定义

can you resolve this and explain what happen in this my route.. 您能解决这个问题并解释在此路线中会发生什么吗?

thank's for your concern 感谢你的关心

Your problem is that you cannot achieve the object returned from within route in action handler directly like this.get('profileFormsAttributes') ; 您的问题是您无法像this.get('profileFormsAttributes')那样直接在动作处理程序中实现从路由内返回的对象; hence your setting does not work. 因此您的设置不起作用。

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

Even above two statements does not work; 甚至以上两个语句也不起作用; because you cannot retrieve model or controller like this. 因为您不能像这样检索modelcontroller

You have two options; 您有两个选择; either you need to save what you are going to return from model directly within model hook with this.set('model', model) or you can achieve it with this.controllerFor(this.get('routeName')).get('model') 您需要使用this.set('model', model)model挂钩中直接保存要从模型返回this.set('model', model)或者可以使用this.controllerFor(this.get('routeName')).get('model')

I would recommend the second approach for your case. 我会为您的情况推荐第二种方法。 Please take a look at the following twiddle that I have prepared to illustrate the case for you. 请查看以下我准备为您说明的旋转技巧

Please take a look at index.js where foo attribute of object returned from model hook is set with 请看一下index.js ,其中从model挂钩返回的对象的foo属性设置为

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

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM