Last active
          August 29, 2015 14:11 
        
      - 
      
 - 
        
Save simonexmachina/cb49e3a396c6c35e276c to your computer and use it in GitHub Desktop.  
    Alternative implementations of some Ember.computed functions to avoid issues with broken ArrayComputed and ReduceComputed
  
        
  
    
      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
    
  
  
    
  | Em.computed.map = function(key, callback) { | |
| return Em.computed(key + '.[]', function() { | |
| return Em.get(this, key).map(callback.bind(this)); | |
| }); | |
| }; | |
| Em.computed.mapBy = function(key, property) { | |
| return Em.computed(key + '.@each.' + property, function() { | |
| return Em.get(this, key).mapBy(property); | |
| }); | |
| }; | |
| Em.computed.max = function(key) { | |
| return Em.computed(key + '.[]', function() { | |
| return max(Em.get(this, key)); | |
| }); | |
| }; | |
| Em.computed.filterBy = function(key, property, value) { | |
| return Em.computed(key + '.@each.' + property, function() { | |
| return value === undefined ? | |
| Em.get(this, key).filterBy(property) : | |
| Em.get(this, key).filterBy(property, value); | |
| }); | |
| }; | |
| export function max(array, callback) { | |
| if (!callback) { callback = x => x; }; | |
| if (typeof callback !== 'function') { throw new TypeError(); } | |
| var withNums = array.map(obj => ({ | |
| obj: obj, | |
| num: callback(obj) | |
| })); | |
| var maxWithNum = withNums.reduce( | |
| (accum, item) => accum.num > item.num ? accum : item, | |
| { obj: null, num: Number.NEGATIVE_INFINITY }); | |
| return maxWithNum.obj; | |
| } | |
| export default null; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment