I found the solution here is not very satisfactory. Although the approach is quite interesting, it fails when the user selects an account later and the browser allows the use of stored credentials (for example, if multiple credentials are stored). This also failed when you started entering the password and deleted to return to the original password (at least in Firefox). Also, I didn’t really like the timeout to give the browser time - it’s just not so nice.
My solution: which is not really one, but I thought I was anyway
Just update our model manually before you login in the submit callback. Using jQuery, something like self.password($("#password").val()) should do this.
Alternatively, using existing bindings, triggering a change event also seems to work. $("#password").change() .
Pros:
- Designed for credential fields only, so maybe one time for your site.
- simple and clean - one or two lines in the right place
- always works reliably, no matter which browser, credential setting or usage pattern
Against:
- good separation is torn again. Knockout.js provides
- - this is not a solution, but a workaround
I will stick to this because I found it just reliable work. It would be nice to say that Knockout revises the bindings directly, rather than saving the value back manually or triggering it through a change event. But I haven’t found anything yet.
Just think a little ahead - the same problem should occur when the browser automatically completes any form (for example, as an address) - this means that some general function that performs the above would be nice (perhaps by triggering a change trigger on each input field forms)
Edit: Quick code demonstrating the idea
HTML:
<form id="myForm" data-bind="submit: login"> Email: <input type="text" data-bind="value: email" /><br/> Password: <input type="password" data-bind="value: password" /><br/> <button type="submit">Login</button> </form>
And Javascript:
function ViewModel() { var self = this; self.email = ko.observable(""); self.password = ko.observable(""); self.login = function() { $("#myForm").find("input").change();
Michael
source share