Composable Computed Properties

Howdy Embereños!

This PR brings Composable Computed Properties to live. What is CCP? Well, it is a computed property that combines other computed properties :smile:

Example:

var sort = Ember.computed.sort,
    union = Ember.computed.union,
    equal = Ember.computed.equal,
    mapBy = Ember.computed.mapBy,
    not = Ember.computed.not,
    compare = Ember.compare,
    and = Ember.computed.and;

// Example 1

var Person = Ember.Object.extend(
  napTime: and(
      equal('state', 'sleepy'),
      not('thirsty'),
      not('hungry')
  )
);

var person = Person.create({
  state: 'sleepy',
  thirsty: true,
  hungry: false
});

person.get('napTime') => false
person.set('thirsty', false)
person.get('napTime') => true

// Example 2

var Kitties = Ember.Object.extend(
  allKitties: sort(
    union(
      mapBy('whiteKitties', 'name'),
      mapBy('blackKitties', 'name')
  ), compare
 )
);

var kitties = Kitties.create({
   whiteKitties: Ember.A([
     { name: 'whiskeys' },
     { name: 'bagels' }
   ]),
   blackKitties: Ember.A([
     { name: 'little boots' }
   ]),
});

kitties.get('allKitties') => ['bagels', 'little boots', 'whiskeys']

Let me know what you guys think.

I heard some people complain that examples are hard to read. I’m open to any suggestions on improving it :smile:

4 Likes

Alex, I posted a comment w/ some code examples here https://github.com/emberjs/ember.js/pull/3696. Let me know if you’d like that copied over here. I think the chaining style leads to much easier to reason about code than the nested “LISP” style of function composition.

1 Like

yup, I saw it, thanks for your feedback!