-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE ds-boolean-transform-allow-null] allow null for boolean #4022
Conversation
@pangratz During the team discussion someone suggested passing an options hash to |
Seems good to me. But for this to work emberjs/rfcs#1 (also see discussion in #3111) needs to be go'ed and implemented, since currently the hash is not passed to transformations. Would the eventual default in |
Alright, emberjs/rfcs#1 has been accepted. Once that RFC is implemented I will update this PR so the |
Alrighty, I rebased onto latest export default Model.extend({
leBool: attr('boolean', { allowNull: true })
}); |
@@ -38,6 +50,14 @@ export default Transform.extend({ | |||
}, | |||
|
|||
serialize(deserialized) { | |||
if (isEnabled('ds-transform-pass-options')) { | |||
if (isEnabled('ds-boolean-transform-allow-null')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be checking the 2nd argument for { allowNull: true }
?
This feature allows `null` / `undefined` values for `DS.attr('boolean')` attributes. Currently `DS.BooleanTransform` converts those values to `false`; which means that a `DS.attr('boolean')` cannot be set to `null` from the server. Other transforms (`string`, `number`) do allow `null` values, so this is an inconsistency. Current behavior of `DS.attr('boolean')`: | incoming | DS | serialized | |-------------|---------|------------| | `null` | `false` | `false` | | `undefined` | `false` | `false` | Behavior of `DS.attr('boolean')`, having `ds-boolean-transform-allow-null` feature enabled and having an attribute `DS.attr('boolean', { allowNull: true })`: | incoming | DS | serialized | |-------------|---------|------------| | `null` | `null` | `null` | | `undefined` | `null` | `null` | --- Note that this feature only works if `ds-transform-pass-options` is enabled as well, since passing the options from `DS.attr` to the transform is added with that feature.
@bmac I somehow forgot to commit the changes considering the I - once again - updated the PR; now it should be ready for review 😉 |
Wouldn't |
I think this wouldn't work since |
var type = typeof serialized; | ||
|
||
if (isEnabled('ds-transform-pass-options')) { | ||
if (isEnabled('ds-boolean-transform-allow-null')) { | ||
if (isNone(serialized) && options.allowNull === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use Ember.get(options, 'allowNull')
in case options
is undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.
[FEATURE ds-boolean-transform-allow-null] allow null for boolean
This is a great adjustment! 🎆 @bmac @fivetanley for 3.0, would you mind flipping the default of the option (or removing the option altogether)? In our app I'd say 99% of boolean values across all our models would use the allowNull option. Also, it's inconsistent with string, number etc. where no option is required. |
NOTE: this description is not completely accurate anymore, since the implementation changed so the
allowNull
flag needs to be passed to the options for the attribute. See discussion below for further context.This feature allows
null
/undefined
values forDS.attr('boolean')
attributes. Currently
DS.BooleanTransform
converts those values tofalse
; which means that aDS.attr('boolean')
cannot be set tonull
from the server. Other transforms (
string
,number
) do allownull
values, so this is an inconsistency between the transforms.
The new feature
ds-boolean-transform-allow-null
changes this behaviorso
null
/undefined
are converted tonull
. Since this is a semverbreaking change, a corresponding deprecation warning should be added
before this is accepted in ember data.
Current behavior of
DS.attr('boolean')
:null
false
false
undefined
false
false
Behavior of
DS.attr('boolean')
, havingds-boolean-transform-allow-null
feature enabled:null
null
null
undefined
null
null
Note that this feature only works if
ds-transform-pass-options
isenabled as well, since passing the options from
DS.attr
to thetransform is added with that feature.
This addresses #3785.
TODO
allowNull
flag