2017-08-16 46 views
0

我目前有包含一个计算一个别名模型,如下:Ember.js:从部件的javascript访问计算别名

model: DS.belongsTo('device-model'), 
manufacturerName: Ember.computed.alias('model.manufacturer.name') 

我然后有时调用的组件,如下所示:

{{my-component model=model}} 

现在,在组件的Handlebars模板中,我可以使用{{model.manufacturerName}}轻松访问计算属性,但是在my-component.js中,我无法使其正常工作。我曾尝试用:

console.log(this.get('model').get('manufacturerName')) 

然而,输出为undefined。到目前为止,我可以从我-component.js得到制造商的名称的唯一方法是:

this.get('model').get('model') 
.then((model) =>{ 
    return model.get('manufacturer') 
}) 
.then((manufacturer) => { 
    console.log(manufacturer.get('name')) 
}) 

所以,我想知道什么是车把模板做,我不能在它的JavaScript对应呢?看起来Handlebars模板遵循承诺,而当涉及到组件的javascript时,我必须手动完成。

谢谢!

回答

-1

我认为这个问题是因为你的belongsTo关系。因为{async:true}是Ember中关系的默认值。因此只有在您实际请求时才会获取相关实体。这意味着您的模型未加载,这意味着您的制造商名称未加载,因为它是model.manufacturerName.name的别名。

+0

感谢您的反馈意见。我明白你的意思,但问题是,为什么Handlebars模板可以访问该属性,但是它的JavaScript对应模块却不能?我必须在组件的JavaScript中做额外的步骤? – finferflu

+0

这很奇怪,我从来没有遇到过这个。要么他们都工作,要么他们都不工作。我怀疑你的控制台日志是在加载模型“设备模型”之前发生的。并且hbs中的{{model.manufacturerName}}会刷新或加载。我会尝试在您调用像this.store.findAll('device-model')这样的组件之前在您的视图的路线中加载'device-model', – feiiiiii

+0

我已经这么做了,在我的视图中,那。 – finferflu