Created
March 8, 2016 00:44
-
-
Save Asherlc/cc438c9dc13912618b8b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import DS from 'ember-data'; | |
const { | |
A, | |
Helper, | |
computed, | |
defineProperty, | |
get, | |
isArray, | |
observer, | |
set, | |
isPresent | |
} = Ember; | |
const groupFunction = function() { | |
const groups = new A(); | |
const items = get(this, 'array'); | |
const property = get(this, 'byPath'); | |
const promises = items.map(function(item) { | |
const value = get(item, property); | |
return Ember.RSVP.resolve(value).then((resolvedValue) => { | |
let group = groups.findBy(property, resolvedValue); | |
if (isPresent(group)) { | |
get(group, 'items').push(item); | |
} else { | |
group = { [property]: resolvedValue, items: [item] }; | |
groups.push(group); | |
} | |
}); | |
}); | |
return DS.PromiseArray.create({ | |
promise: Ember.RSVP.all(promises).then(() => { return groups; }) | |
}) | |
}; | |
export default Helper.extend({ | |
compute([byPath, array]) { | |
set(this, 'array', array); | |
set(this, 'byPath', byPath); | |
return get(this, 'content'); | |
}, | |
byPathDidChange: observer('byPath', function() { | |
let byPath = get(this, 'byPath'); | |
if (byPath) { | |
defineProperty(this, 'content', computed(`array.@each.${byPath}`, groupFunction)); | |
} else { | |
defineProperty(this, 'content', null); | |
} | |
}), | |
contentDidChange: observer('content', function() { | |
this.recompute(); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment