javascriptember.jsember-dataember-routerember-controllers

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

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.

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


Solution

  • Your problem is that you cannot achieve the object returned from within route in action handler directly like 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.

    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')

    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

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

    I hope this helps.