javascript - Ember 需要模型中一个 Promise 的数据用于 model() 中的另一查询

标签 javascript ember.js

因此,该应用程序所有路线的模型设置如下:

  model(params) {
    let config = this.get('store').findRecord(params.config_type, params.config_id);
    let c = Ember.RSVP.hash({config});
    console.log('aa');
    console.log(c);
    let devices = this.get('store').findAllOfType('device', ENV.CFG_TYPE[params.config_type]);
    let licenses = this.get('store').findAll('license');

    return Ember.RSVP.hash({
      configType: params.config_type,
      config: config,
      devices: devices,
      licenses: licenses
    });
  },

我需要更改设备查询以使用第一个查询返回的配置中保存的辅助条件。唯一的问题是这尚未解决并且其中没有数据。

当我注销 c 时,我看到 _results 属性最初未定义,然后当我展开它时,它显示配置对象。

我意识到这是因为 promise 尚未解决,并且会在未来某个时间解决,但我需要这些数据来获取正确的设备。我不想将它们作为查询参数传递,因为它们是我需要的两个单独的数据。

我想我可以在配置行上执行 .then() 并返回其中的 Ember.RSVP.hash 但这会将其返回到 model() 并且我不确定如何从那里返回它,或者如果它甚至会从那里返回,或者配置现在等于 RSVP 哈希而不是配置 promise /对象。

我的选择是:

  1. 找到一种方法以某种方式将其从模型中已具有相同配置对象的一条路由传递到此路由,而不使用查询参数

  2. 以某种方式在第一个查询的回调中设置整个模型(findRecord())

完全不知道如何做到这一点。

最佳答案

我试过了,请看看这个,

model(params) {
        return this.get('store').findRecord(params.config_type, params.config_id).then(resultantConfig => {
            //resultantConfig use this to get exact property which is going to be used for the below queries.
            return Ember.RSVP.hash({
                configType: params.config_type,
                config: resultantConfig,
                //findAllOfType - i am not heard of this method, i guess it would query, as you need to set condition.
                devices: this.get('store').query('device', {configType:resultantConfig.config_type}),
                licenses: this.get('store').findAll('license')
            });
        });
    }

关于javascript - Ember 需要模型中一个 Promise 的数据用于 model() 中的另一查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44202946/

相关文章:

javascript - 使用 JavaScript 在文本区域中的选择之前和之后插入文本

javascript - Node js http服务器接受POST并接受JSON

ember.js - 将服务注入(inject) Ember 对象 [不是 Ember Controller ]

javascript - 从 jQuery 选择器 .html() 中删除 Ember.js 脚本标签

javascript - 是否可以在 JavaScript 中覆盖按键重复延迟?

Javascript If BOOL 表达式来自字符串

javascript - 从另一台服务器读取 Node.js 中的大文件

javascript - 在 Sails.js 上获取请求 Ember.js

ember.js - MODEL_FACTORY_INJECTIONS 和夹具

javascript - 如何使用 svg.js 制作 Ember 组件?