Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember: are model initial values null or empty string?

I am confused about the initialisation values of a new model.

When creating a new record, it seems that all properties have "null" as value.

As soon as you type something in the textfield (bound to the model), and then delete the typed value (thus again an empty textfield), the value of the properties becomes emptystring.

Is this the expected behaviour ?

There is a difference for REST API's whether they receive null or "" ...

like image 782
cyclomarc Avatar asked Nov 28 '25 17:11

cyclomarc


1 Answers

if your atribute is DS.attr('string'), so this is correct. Doesn't matter if the string is null or empty, this just represent that the text isn't present. Thinking in programming language you would be aware of the null, because you receive error when accessing methods. But in business rules, doesn't matter if the string is null or empty, so this is a common pattern, for example:

if (name == null || name.length == 0) {
  // name is missing, prompt the user ...
} 

And this is a rare situation:

if (name == null) {
  // some logic here
} else if(name == "") {
  // other logic here
}

About the record creating. When you create it, the values is null. But when binding to a text field, for example:

{{input type="text" valueBinding="field"}} 

You have this relationship:

model.field <-> textfield.value

When you set something in model.field, it will be syncronized with the textfield and vice-versa. But in the case of editing the textfield, the value will always be a string, this is the reason that you receive a empty string, instead of null, when clearing the textfield.

like image 158
Marcio Junior Avatar answered Dec 01 '25 18:12

Marcio Junior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!