javascript - 无法从 ember 中的路线操作获取模型

标签 javascript ember.js ember-data ember-router ember-controllers

我只想刷新 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');

以上两条语句也不起作用;因为您无法像这样检索 modelcontroller

你有两个选择;您需要使用 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/

相关文章:

jsp - 如何在 jsp 文件中包含 ember 应用程序并使用 tomcat 提供服务

javascript - 如何在 Controller 的 Ember.js 单元测试中设置模型数据

javascript - Angular Flot - 通过 Alpha 传递十六进制颜色在 Google Chrome 中不起作用

javascript - 为什么每次滑动后页面都会向上滚动以及如何防止这种情况?

javascript - rails 2 : RJS - Javascript events are not working when on page updates remotely

javascript - 在 asp.Net LinkBut​​ton OnClientClick 上触发 html5 表单验证

javascript - 如何扩展 EmberJS handlebars 输入助手以支持没有值的属性?

javascript - Ember - 计算属性未触发/更新

ember.js - Ember (data) linkTo 不传递模型,只需获取新模型

ember.js - Ember 数据 1.0.0 Beta : RESTAdapter endpoint customization no longer works