我有一个可以支持多种语言的应用程序。我的每个用户都有自己的一套“支持的语言”。假设我的用户支持英语,法语和西班牙语,他可以为自己的商店创建类别,并且必须输入3种语言的名称。但是,当我显示类别列表时,我只想在标签中显示所选的语言名称。
我在控制器中做了以下功能,它可以完美地工作:
App.CategoryController = Ember.ObjectController.extend({
needs: ['settings'],
currentLocale: Ember.computed.alias('controllers.settings.currentLocale'),
name: function() {
var translations = this.get('category_translation_ids').filterBy('locale', this.get('currentLocale'));
Ember.assert("The selected language does not exist", translations.length === 1);
return translations[0].get('name');
}.property('[email protected]')
});
我的问题是当我添加新类别时:
App.CategoriesAddController = Ember.ObjectController.extend({
needs: ['categories', 'settings'],
newRecord: function() {
scope = this;
record = this.store.createRecord( 'category',
{
active: true,
})
record.save().then(function(record) {
scope.get('controllers.settings').get('supportedLocale').forEach(function(item, index, enumerable) {
translation = scope.get('store').createRecord( 'categoryTranslation',
{
locale: item,
name: 'Nom',
description: '',
meta_description: ''
});
record.get('category_translation_ids').pushObject(translation);
translation.save();
});
});
},
});
一旦执行“ createRecord”,便会创建我的类别,并且由于我尚未插入翻译而无法执行计算的属性“名称”。我不知道我是否做对了,因为这是我在余烬中的第一个项目。自从过去4-5个小时以来,我一直在解决这个问题,但没有取得太大的成功。任何帮助将不胜感激。
听起来您有一个显示所有类别的模板,并且类别的项目控制器为CategoryController
。大概是这样的:
{{#each category in controller}}
{{category.name}}
{{/each}}
我确定您正在发生的事情是,当控制器尝试在控制器上进行计算name
时,它崩溃了。有几种方法可以完成此操作,一种是在类别不是新的情况下进行过滤。
{{#each category in controller}}
{{#unless category.isNew}}
{{category.name}}
{{/unless}}
{{/each}}
您也可以基于记录的状态进行过滤,尽管该状态经常更改并且存在许多不同的状态。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句