Chi*_*Lam 4 handlebars.js ember.js
我试图在把手中循环计算属性数组.在这个例子中,我可以为普通数组,但不是计算数组:http: //jsfiddle.net/gh7Qr/
在句柄中循环计算属性的正确语法应该是什么?
对的,这是可能的.但是你忘记了return
你的计算数组,你必须添加cacheable()
到计算属性,它返回一个对象而不是一个基元.否则你将遇到一个无限循环(见讨论https://github.com/emberjs/ember.js/issues/38)另外看看Gordon Hempton 关于当前Ember.js陷阱的优秀博客文章,其中包括计算属性.但是,自提交626d23f以来,已经解决了可缓存的问题.
您的代码的更正示例如下:http://jsfiddle.net/gh7Qr/4/
把手:
<script type="text/x-handlebars" >
{{#each App.games}}
{{this}}
{{/each}}
{{#each App.gamesA}}
{{this}}
{{/each}}
</script>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
App = Ember.Application.create({
games: [1, 2, 3],
gamesA: Em.computed(function() {
return this.get('games').map(function(game) {
return game + 'a';
})
}).property('games').cacheable()
});?
Run Code Online (Sandbox Code Playgroud)