Skip to content
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

Default values are not overridden when constructing a model #186

Closed
1 task done
nandi95 opened this issue Jan 27, 2022 · 1 comment · Fixed by #187
Closed
1 task done

Default values are not overridden when constructing a model #186

nandi95 opened this issue Jan 27, 2022 · 1 comment · Fixed by #187
Labels
bug Something isn't working has workaround There is a way to achieve the aim

Comments

@nandi95
Copy link
Collaborator

nandi95 commented Jan 27, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Search terms

field
property
attribute
default value
constructing
new model
magic access
descriptors
accessor
mutator
constructing

Current Behavior

If a model has default values like so:

export default class User extends Model {
  public myAttribute: number = 1;
}

when constructing a class with myAttribute value passed in as 2 new User({ myAttribute: 2 }) key will still be 1.
Furthermore, casting and mutators will not work either.

Expected Behavior

Even if there are default values, the attribute should still be set to the passed value when constructing the class.

Environment

- OS: darwin
- Node: 17.4.0
- npm: 8.3.1

Anything else?

This is due to js first constructing the parent class and properties defined in the subclass will only be actually set when it comes to constructing the subclass. By this time the property descriptors have already been set up and now being overwritten.

Workaround

Avoid constructing classes by using the create method or construct the class and call the fill with the intended attributes on the instance

Reproduction

No response

@nandi95 nandi95 added bug Something isn't working has workaround There is a way to achieve the aim labels Jan 27, 2022
@nandi95
Copy link
Collaborator Author

nandi95 commented Jan 27, 2022

This is related to #180 as the issue stems from setting default values.

nandi95 added a commit to nandi95/framework-1 that referenced this issue Jan 27, 2022
This will allow setting up descriptors for default values
Resolves upfrontjs#186
@nandi95 nandi95 linked a pull request Jan 28, 2022 that will close this issue
7 tasks
nandi95 added a commit that referenced this issue Jan 28, 2022
## Fix
* fix(attributes)(**BREAKING CHANGE**): moved constructor logic into `create`
  * This will allow setting up descriptors for default values
  * Resolves #186

### Docs
* docs(model): added missing `create` documentation
* docs: simplified pagination recipe
* docs(model): fixed grammar

### Refactor
* refactor(collection): updated `_newInstance` typing to not use any
* refactor(relations): moved `relations` into its handling class
* refactor(internal): added response handling method overrides

## Chore
* chore: incremented package version
* chore(deps-dev): updated lint-staged

### Style
* style(api-calls): moved method call to new line
* style(api-calls): fixed eslint issue
* style(api-calls): fixed eslint issue in test

### Continuous Integration
* ci: wrapped dotfile in quotes
@nandi95 nandi95 closed this as completed Jan 28, 2022
nandi95 added a commit that referenced this issue Jan 28, 2022
## Fix
* fix(attributes)(**BREAKING CHANGE**): moved constructor logic into `create`
  * This will allow setting up descriptors for default values
  * Resolves #186

### Docs
* docs(model): added missing `create` documentation
* docs: simplified pagination recipe
* docs(model): fixed grammar

### Refactor
* refactor(collection): updated `_newInstance` typing to not use any
* refactor(relations): moved `relations` into its handling class
* refactor(internal): added response handling method overrides

## Chore
* chore: incremented package version
* chore(deps-dev): updated lint-staged

### Style
* style(api-calls): moved method call to new line
* style(api-calls): fixed eslint issue
* style(api-calls): fixed eslint issue in test

### Continuous Integration
* ci: wrapped dotfile in quotes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has workaround There is a way to achieve the aim
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant