telerik / kendo-ui-core

An HTML5, jQuery-based widget library for building modern web apps.
http://www.telerik.com/kendo-ui
Other
2.54k stars 1.91k forks source link

kendo.data.Model field defaultValues are not applied when partial values are supplied #655

Closed AdamAndersonFalafelSoftware closed 9 years ago

AdamAndersonFalafelSoftware commented 9 years ago

Please see this working example: http://dojo.telerik.com/Ohiso/4

When a model has two fields with default values defined, the defaults are only applied if no values object is passed to the constructor. If you pass an object containing only one field to the constructor, the other field is not initialized with the default.

rkonstantinov commented 9 years ago

This is the designed behavior. The default values are applied only when there are no value passed into the constructor. When data is provided, it is assumed that you want to wrap an existing record, thus it should have all of the appropriate values already set.

AdamAndersonFalafelSoftware commented 9 years ago

Forgive me, but I don’t see how this allegedly designed behavior is beneficial to anyone. If the Model would merge/extend default properties onto a passed object, then when the passed object already has all its fields defined then nothing would change. The way it stands currently, if you pass in a partial object with only the known/non-default properties initialized, you end up with an invalid Model instance because it’s missing fields. If the Model initializer merged in missing properties with the defaults, then the result would be a valid Model instance with no missing properties, which seems like an obviously preferable outcome. What is the justification for this design when it appears so obviously undesirable?

From: Rosen Konstantinov [mailto:notifications@github.com] Sent: Monday, June 8, 2015 11:26 AM To: telerik/kendo-ui-core Cc: Adam Anderson Subject: Re: [kendo-ui-core] kendo.data.Model field defaultValues are not applied when partial values are supplied (#655)

This is the designed behavior. The default values are applied only when there are no value passed into the constructor. When data is provided, it is assumed that you want to wrap an existing record, thus it should have all of the appropriate values already set.

— Reply to this email directly or view it on GitHubhttps://github.com/telerik/kendo-ui-core/issues/655#issuecomment-110039938.

rkonstantinov commented 9 years ago

Hi Adam,

As I have mentioned there was never an intention for merging the default values with those which are provided via the constructor when given plain object is wrapped into a Model.

The default values are only used when a new, empty instance of the Model is instantiated. This is also visible from the Model Class constructor code.

In case you require such merging functionality, you should consider extending the Model type and use this new subclass where appropriate when constructing the model instances in your application.

AdamAndersonFalafelSoftware commented 9 years ago

Yes, I had traced the behavior down to that line and since I thought that the current behavior was so obviously undesirable that the highlighted line must have been written in error. ☺ I still haven’t heard a good reason justifying why this design decision was made, but it’s true enough that I can extend Model myself to achieve the behavior I want. I think Kendo is a really nice framework and I’ll keep on loving it in spite of this minor complaint. ☺ Thanks for taking the time to respond!

From: Rosen Konstantinov [mailto:notifications@github.com] Sent: Monday, June 8, 2015 1:47 PM To: telerik/kendo-ui-core Cc: Adam Anderson Subject: Re: [kendo-ui-core] kendo.data.Model field defaultValues are not applied when partial values are supplied (#655)

Hi Adam,

As I have mentioned there was never an intention for merging the default values with those which are provided via the constructor when given plain object is wrapped into a Model.

The default values are only used when a new, empty instance of the Model is instantiated. This is also visible from the Model Class constructor codehttps://github.com/telerik/kendo-ui-core/blob/master/src/kendo.data.js#L662.

In case you require such merging functionality, you should consider extending the Model type and use this new subclass where appropriate when constructing the model instances in your application.

— Reply to this email directly or view it on GitHubhttps://github.com/telerik/kendo-ui-core/issues/655#issuecomment-110090003.