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 JSON number type converts to string upon edit #287

Closed
brettveenstra opened this issue Jan 14, 2016 · 5 comments
Closed

Binding JSON number type converts to string upon edit #287

brettveenstra opened this issue Jan 14, 2016 · 5 comments

Comments

@brettveenstra
Copy link

While trying to implement an object comparison (using the areEqual function from the app-contacts project), discovered that once a number property is bound and edits applied (input type=text), it mutates to a string, breaking straight-forward comparison efforts.

The binding infrastructure seems to be the place to address this effect, maybe the ShadowDOM?

Here's some example code:

// Customer JSON
{
  customerName: 'Foo',
  customerNo: 12344045,
  customerId: 1,      // read-only
  balanceThreshold: 1000,
  status: 'Active'
};



// View Model
activate(params) {
    this.customerService.get(params.id).then((data) => {
      this.currentCustomer = data;
      this.originalCustomer = JSON.parse(JSON.stringify(data));
      this.isDirty = false;

      Object.keys(this.currentCustomer).forEach((key) => {
        this.bindingEngine.propertyObserver(this.currentCustomer, key).subscribe((newValue, oldValue) => {
          this.isDirty = !areEqual(this.originalCustomer, this.currentCustomer);
          if (this.isDirty)
          {
            // console.dir() against the original and current objects shows the effect
          }              
        });
      });
}

// utility function
export function areEqual(obj1, obj2) {
  return Object.keys(obj1).every((key) => obj2.hasOwnProperty(key) && (obj1[key] === obj2[key]));
}

So changes to balanceThreshold or customerNo trigger a the call back but the isDirty will always stay true.

@fopsdev
Copy link

fopsdev commented Jan 14, 2016

yeah i also recognised that.
i've fixed it by using a valueconverter:

export class IntegerFromInputValueConverter {
  fromView(value) {
    return parseInt(value);
  }
}

would be cool if the binding system would automatically do that because if i bind an integer i also expect an integer back...

@EisenbergEffect
Copy link
Contributor

We have some ideas about how this could be done in the future. The basic problem is that inputs of type text coerce the value to string, which is out of our control. But we have some ways we think we can help do it on the model side.

@fopsdev
Copy link

fopsdev commented Jan 15, 2016

ok good to know
for now i will provide global valueconverters for all my input types.
why for all?
because i would like to set the viewModel to dirty if there is something coming back from the view (fromView)
But how can i access the viewModel from inside the valueconverter?

@jdanyow
Copy link
Contributor

jdanyow commented Jan 16, 2016

@jdanyow jdanyow closed this as completed Jan 16, 2016
@brettveenstra
Copy link
Author

thanks for the reference @jdanyow

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

No branches or pull requests

4 participants