jquery - Validating an autocomplete selection on a ko observable value -
i have autocomplete list of values. selected value of selection ko observed variable. need validate value selected list or value typed in, incase did not select value list.
it appears right when type letter , select value list, validation call fired letter typed , called @ same time value selected.
is there approach validating value user selected or typed not race condition?
here code:
html
<div> <label for="assignedto">assigned to: </label> <input id="assignedto" placeholder="assigned to" data-bind="ko_autocomplete:{source: $parent.users(), select: addassignedto}, value: assignedto"/> <span class="error" data-bind="text: assignedtoinvalid"></span> </div>
model
self.assignedtoinvalid= ko.observable(); self.assignedto= ko.observable(); self.addassignedto= function (event, ui) { if (!isnullorempty(self.assignedto())) { isuservalid(ui.item.label, self.assignedtovalidated, self.assignedtonotvalid); } }; self.assignedtovalidated= function (user) { self.assignedto(user); }; self.assignedtonotvalid= function (error) { self.assignedtoinvalid("invalid user"); }; self.assignedto.subscribe(function() { if (!isnullorempty(self.assignedto)) { isuservalid(self.assignedto(), self.assignedtovalidated, self.assignedtonotvalid); } });
javascript
function isuservalid(username, successfunc, failurefunc) { var url = '/controller/method'; var data = [{ name: 'username', value: username}]; postjson(url, data, successfunc, failurefunc); }
ko binding handler
ko.bindinghandlers.ko_autocomplete = { init: function (element, params) { $(element).autocomplete(params()); }, update: function (element, params) { $(element).autocomplete("option", "source", params().source); } };
Comments
Post a Comment