Skip to content

Latest commit

 

History

History
93 lines (67 loc) · 1.67 KB

0011-improved-cp-syntax.md

File metadata and controls

93 lines (67 loc) · 1.67 KB
stage start-date release-date release-versions teams prs project-link meta
discontinued
2014-09-30 00:00:00 UTC
2014-10-28 00:00:00 UTC
ember-source
v1.8.0
framework
accepted
ember-issue

Summary

Improve computed property syntax

Motivation

Today, the setter variant of CP's is both confusing, and looks scary as sin. (Too many concepts must be taught and it is too easy to screw it up.)

Detailed design

today:

fullName: Ember.computed('firstName', 'lastName', function(key, value) {
  if (arguments.length > 1) {
    var names = value.split(' ');
    this.setProperties({
      firstName: names[0],
      lastName: names[1]
    });
    return value;
  }

  return this.get('firstName') + ' ' + this.get('lastName');
});

Tomorrow:

fullName: Ember.computed('firstName', 'lastName', {
  get: function(keyName) {
    return this.get('firstName') + ' ' + this.get('lastName');
  },

  set: function(keyName, fullName, oldValue) {
   var names = fullName.split(' ');

   this.setProperties({
     firstName: names[0],
     lastName: names[1]
   });

   return fullName;
  }
});

Notes:

  • we should keep Ember.computed(fn); as shorthand for getter only
  • get xor set variants would also be possible.
  • { get() { } } is es6 syntax for { get: function() { } )

Migration

  • 1.x support both, detect new behaviour by testing if the last arg is not null and typeof object
  • 1.x+1 deprecate if last arg is a function and its arity is greater than 1

Drawbacks

N/A

Alternatives

N/A

Unresolved questions

None