-1

I have a ViewModel and i use that with ericmbarnard / Knockout-Validation. In it i have this field:

self.checksum = ko.observable().extend({required: {message: " * Required"}});

In my html, this:

<input type="text" id="txtCheckSum" name="txtCheckSum" data-bind="value: checksum"/>

And I add the "input file" value with a javascript code. In this javascript code, I calculate the md5 file and then fill the field txtCheckSum. But unfortunately, this does not automatically fills my ViewModel.

So, when I call the check "viewModel.errors().length == 0", I have an error like that is empty.

Help?

Eduardo
  • 1,698
  • 4
  • 29
  • 48

2 Answers2

1

As mentioned in the comments the point of KO is to separate your data from your view. By using the jquery selectors to update the value of inputs you are essentially using KO wrong. In very few circumstances is plain jquery needed.

The correct approach would be to update the observable itself.

vm.checksum("md5 code");

Then you can eliminate the $("#txtCheckSum").val/trigger all together.

madcapnmckay
  • 15,782
  • 6
  • 61
  • 78
0

I found the solution

In the Javascript code, I put this:

    $("#txtCheckSum").val('md5 code');
    $('#txtCheckSum').trigger('change');

And my viewmodel accepted the changes.

glenstorey
  • 5,134
  • 5
  • 39
  • 71
Eduardo
  • 1,698
  • 4
  • 29
  • 48
  • 1
    You should really be updating the checkSum observable. In KO the use of $(selectors) should be eliminated if possible since it violates the separation of concerns that KO gives you. – madcapnmckay May 24 '12 at 15:18