From 1ab7ff4e414709520a358a5a1d9726b20f00a617 Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Thu, 30 Oct 2014 23:33:43 -0400 Subject: [PATCH 1/5] initial draft --- 0000-improved-cp-syntax.md | 81 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 0000-improved-cp-syntax.md diff --git a/0000-improved-cp-syntax.md b/0000-improved-cp-syntax.md new file mode 100644 index 0000000000..cf90d5aca1 --- /dev/null +++ b/0000-improved-cp-syntax.md @@ -0,0 +1,81 @@ +- Start Date: 2014-19-30 +- RFC PR: (leave this empty) +- Ember Issue: (leave this empty) + +# Summary + +Improve computed property syntax + +# Motivation + +Today, the setter variant of CP's is both confusing, and looks scary as sin. +(To many concepts must be taught and it is to easy to screw it up.) + +# Detailed design + +today: +------ + +```js +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: +--------- + +```js +fullName: Ember.computed('firstName', 'lastName', { + get(keyName) { + return this.get('firstName') + ' ' + this.get('lastName'); + }, + + set(keyName, fullName) { + 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 then 1 + + +# Drawbacks + +N/A + +# Alternatives + +N/A + +# Unresolved questions + +* do setters with the new syntax get the 3rd arg of `oldValue` provided? From 898a494eeaa7be1cb2867cc6c5a5a0f1cd5910aa Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Sun, 2 Nov 2014 12:35:36 -0500 Subject: [PATCH 2/5] Update 0000-improved-cp-syntax.md --- 0000-improved-cp-syntax.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/0000-improved-cp-syntax.md b/0000-improved-cp-syntax.md index cf90d5aca1..051f8c884d 100644 --- a/0000-improved-cp-syntax.md +++ b/0000-improved-cp-syntax.md @@ -36,11 +36,11 @@ Tomorrow: ```js fullName: Ember.computed('firstName', 'lastName', { - get(keyName) { + get: function(keyName) { return this.get('firstName') + ' ' + this.get('lastName'); }, - set(keyName, fullName) { + set: function(keyName, fullName) { var names = fullName.split(' '); this.setProperties({ From 961c5d1e374d4491c5ea16052f42a6d9de995ad0 Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Sun, 2 Nov 2014 12:42:22 -0500 Subject: [PATCH 3/5] Update 0000-improved-cp-syntax.md --- 0000-improved-cp-syntax.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/0000-improved-cp-syntax.md b/0000-improved-cp-syntax.md index 051f8c884d..5268ab2031 100644 --- a/0000-improved-cp-syntax.md +++ b/0000-improved-cp-syntax.md @@ -40,7 +40,7 @@ fullName: Ember.computed('firstName', 'lastName', { return this.get('firstName') + ' ' + this.get('lastName'); }, - set: function(keyName, fullName) { + set: function(keyName, fullName, sometimesOldValue) { var names = fullName.split(' '); this.setProperties({ @@ -77,5 +77,3 @@ N/A N/A # Unresolved questions - -* do setters with the new syntax get the 3rd arg of `oldValue` provided? From 58049fdb83561a5d70166a0dd9b9437ba4354e4a Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Sun, 2 Nov 2014 12:42:39 -0500 Subject: [PATCH 4/5] Update 0000-improved-cp-syntax.md --- 0000-improved-cp-syntax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0000-improved-cp-syntax.md b/0000-improved-cp-syntax.md index 5268ab2031..9b05d4892b 100644 --- a/0000-improved-cp-syntax.md +++ b/0000-improved-cp-syntax.md @@ -40,7 +40,7 @@ fullName: Ember.computed('firstName', 'lastName', { return this.get('firstName') + ' ' + this.get('lastName'); }, - set: function(keyName, fullName, sometimesOldValue) { + set: function(keyName, fullName, oldValue) { var names = fullName.split(' '); this.setProperties({ From ae07e7b1ce1996686c91250ebb2158a4eff4bce0 Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Sun, 2 Nov 2014 12:43:24 -0500 Subject: [PATCH 5/5] Update 0000-improved-cp-syntax.md --- 0000-improved-cp-syntax.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/0000-improved-cp-syntax.md b/0000-improved-cp-syntax.md index 9b05d4892b..a617931f4e 100644 --- a/0000-improved-cp-syntax.md +++ b/0000-improved-cp-syntax.md @@ -77,3 +77,5 @@ N/A N/A # Unresolved questions + +None