2
我有简单地提供数据作为阵列的服务:功能没有返回正确的值
// services/countries.js
import Ember from 'ember';
export default Ember.Service.extend({
countries: [
{
"name": "Afghanistan",
"code": "AF"
},
....
]
});
,我可以在一个辅助成功地访问:
// helpers/countries.js
export default Ember.Helper.extend({
countries: Ember.inject.service('countries'),
compute(params, hash) {
console.log(this.get('countries.countries'));
return 'test';
}
});
现在我加该服务的功能以搜索给定的国家代码并返回匹配的国家:
// in services/countries.js
...
getByCode: function(code) {
this.get('countries').forEach(function(item) {
if(item.code===code) { // finds the right item
console.log('returning item:');
console.log(item); // outputs the right object
return item; // I expected to have the same item retured..
}
});
return {name:'not found', code: ''};
},
...
当我叫我的助手
// in helpers/countries.js
...
compute(params, hash) {
let country = this.get('countries').getByCode('DE');
console.log(country); // outputs {name: 'not found',..} instead of the found and returned(?) item
return country.name;
}
...
注意的功能,即正确的输出(执行console.log服务)是前“错误”的输出:
// console output
returning item: roles.js:6
Object {name: "Germany", code: "DE", nameLocal: "Deutschland"} hash.js:2
Object {name: "not found", code: ""}
什么也让我好奇是,在控制台的'错误'.js被提及(roles.js - 这是另一种服务,没有这个功能)
所以我的问题是为什么我得到不同的项目返回/输出?
为了完整性: 我的用户这个帮手在我的模板只有一次像这样:
{{#if model.country}}{{countries model.country}}{{/if}}
(这当然也输出了 '错误' 的国家)
灰烬,CLI 1.13 .0 Ember 2.0.1
看起来很有希望!我会测试它,然后接受!非常感谢! – Jeff
测试过,工作像魅力!谢谢!我不知道无法像循环中的普通循环那样打破forEach ...(我使用了第二个版本) – Jeff