47 lines
1.6 KiB
CoffeeScript
47 lines
1.6 KiB
CoffeeScript
mapSum = (target, property) ->
|
|
Ember.computed "#{target}.@each.#{property}", ->
|
|
return 0 unless ary_result = @get(target).mapBy(property)
|
|
return 0 unless ary_result.length
|
|
ary_result.reduce( ((sum, item) -> sum + item), 0)
|
|
|
|
mapFilter = (target, property, filter_by_key) ->
|
|
Ember.computed "#{target}.@each.#{property}", filter_by_key, ->
|
|
@get(target).filterBy(property, @get(filter_by_key))
|
|
|
|
mapHasMany = (target, has_many_relation) ->
|
|
Ember.computed "#{target}.@each.#{has_many_relation}", ->
|
|
result = Ember.A()
|
|
Ember.RSVP.all(@get(target).mapBy(has_many_relation)).then (targets_has_many_associations) ->
|
|
Ember.run ->
|
|
targets_has_many_associations.forEach (target_has_many_records) ->
|
|
result.pushObjects target_has_many_records.toArray()
|
|
result
|
|
|
|
mapMany = (target, many_attribute) ->
|
|
Ember.computed "#{target}.@each.#{many_attribute}", ->
|
|
result = Ember.A()
|
|
Ember.run =>
|
|
targets = @get(target)
|
|
if Ember.isArray(targets)
|
|
targets.forEach (record) ->
|
|
result.pushObjects record.get(many_attribute)
|
|
else
|
|
targets.then (records) ->
|
|
records.forEach (record) ->
|
|
result.pushObjects record.get(many_attribute)
|
|
result
|
|
|
|
mapMax = (target, property, options={}) ->
|
|
Ember.computed "#{target}.@each.#{property}", ->
|
|
return 0 unless ary_result = @get(target).mapBy(property)
|
|
return 0 unless ary_result.length
|
|
ary_result.reduce (max, item) ->
|
|
Math.max(max, item)
|
|
, -Infinity
|
|
|
|
export { mapSum, mapFilter, mapHasMany, mapMany, mapMax }
|
|
export default Ember.Object.create
|
|
mapSum: mapSum
|
|
mapFilter: mapFilter
|
|
mapHasMany: mapHasMany
|