我已经在stackoverflow中阅读了一些主题,但是我没有找到解决问题的方法...我想将我所有模型中“ progress”字段的总和放入集合中...因此,在我的集合文件中我有:
progressTotal: ->
total = _.reduce(@, ((memo, value) ->
memo + value.get('progress')
), 0)
return total
但是我得到的值是不确定的...为什么?我从这里汲取了灵感:使用ribs.js获取集合(所有模型)的总和,但该解决方案对我不起作用。
有什么建议吗?谢谢你
编辑:好像进度是一个字符串...但在我的数据库中是一个整数。
您应该传递一个models
到reduce
而不是collection的数组,因此只需替换@
为@models
progressTotal: ->
total = _.reduce(@models, ((memo, value) ->
memo + value.get('progress')
), 0)
return total
您也可以使用Backbone.Collection#reduce
方法
progressTotal: ->
return @reduce(((memo, value) ->
memo + value.get('progress')
), 0)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句