我的数据看起来大致如下:
"id": "1",
"slug": "WD",
"name": {
"en": "Working Draft",
"de": "Arbeitsentwurf",
"fr": "Version de travail",
"ja": "草案",
"ru": "Рабочий черновик"
}
我将name
对象传递给组件:
{{title-name name=model.name lang='en'}}
在组件模板中,我想输出翻译次数
<p>Translated {{translationCount}} times.</p>
我在我的组件中尝试了一些不同的东西来提出这个总数,但它们都没有用。我如何计算对象的数量?
export default Ember.Component.extend({
// did not work:
translationCount: Ember.computed.alias('Object.keys(name).length'),
// did not work:
// translationCount: Ember.computed.alias('name.length'),
});
答案 0 :(得分:0)
对它更加明确似乎有用:
export default Ember.Component.extend({
translationCount: Ember.computed('name', function() {
return Object.keys('name').length;
})
});
答案 1 :(得分:0)
查看此ember-twiddle以了解此实现。
申请模板
<h1>Welcome to the {{appName}}</h1>
{{title-name name=data.name lang='en'}}
{{outlet}}
应用程序控制器
import Ember from 'ember';
export default Ember.Controller.extend({
appName:'Stephanie Hobson App',
data: {
id: 1,
slug: 'WD',
name: {
en: 'Working Draft',
de: 'Arbeitsentwurf',
fr: 'Version de travail',
ja: '草案',
ru: 'Рабочий черновик'
}
}
});
title-name.js 组件
import Ember from 'ember';
var { computed, get } = Ember;
export default Ember.Component.extend({
translationCount: computed('name', function() {
var name = get(this, 'name');
return Object.keys(name).length;
})
});
title-name.hbs 组件模板
{{yield}}
{{translationCount}}