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

Binding Changes Property #617

Closed
TonyLugg opened this issue Aug 27, 2017 · 5 comments
Closed

Binding Changes Property #617

TonyLugg opened this issue Aug 27, 2017 · 5 comments
Labels

Comments

@TonyLugg
Copy link

TonyLugg commented Aug 27, 2017

I am submitting a bug report

When I have a view bound to an object and then replace the object with a new object (record), if the properties bound to input boxes are null, they are set to empty string after initial binding. I have created a sample gist here

I have also tested this using routing to load new records, but it gives the same result. I understand that an input box only understands string and not null, but the initial binding should not alter the bound property unless the user changes something. This issue affects my ability to determine if something has changed. Our back end API differentiates null from empty string, because null means the user never entered anything, whereas empty string would indicate there was a value and the user removed it.

@bigopon
Copy link
Member

bigopon commented Sep 1, 2017

A work around is

<input id="txtDepartment" type="text" value.one-way="clientData.department" input.delegate='clientData.department = $event.target.value' />

I don't think in your case it is initial binding anymore, you are jumping from one to next. But it could use some improvement, to notify subscriber about binding information, like

callSubscriber(newValue, oldValue, sourceObj, propertyName)

So subscribers can decide if it should update. In this case, the model observer see that it's coming from an input element, and propertyName is value, so no need to update the model with null / undefined / empty string when oldValue is either of those. This could be related to type inference @EisenbergEffect @jdanyow

@bigopon
Copy link
Member

bigopon commented Sep 1, 2017

@TonyLugg Another work around for you is:

  btnNext_click() {
    // Following two lines will help cleanup things in the DOM, so your model doesn't get updated back
    this.clientData = null;
    this.originalClientData = null;

    this.getClientData(this.getNextId()).then(clientData => {
      this.clientData = clientData;
      this.originalClientData = JSON.parse(JSON.stringify(clientData));
    });
  }

@TonyLugg
Copy link
Author

TonyLugg commented Sep 1, 2017

@bigopon Thank you for your insight and work arounds. I have handled it for now in my function that determines changes. I like your work around to set the objects to null. I'm wondering if it could be done immediately before setting them to the newly downloaded record object or if there would be timing issues. I'll test it and report back.

@jdanyow
Copy link
Contributor

jdanyow commented Sep 17, 2017

html input elements coerce all values to empty string. It would be best to make your model property's initial value empty string rather than null. If you need to use null to represent empty input, you could create a value converter whose toView replaces null with empty string and whose fromView replaces empty string with null.

@jdanyow jdanyow closed this as completed Sep 17, 2017
@TonyLugg
Copy link
Author

Ideally, if the value is initially null and the user does not change it through the input, it should remain null. If the user changes it, the value should be empty string. Not a huge deal. Will work around it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants