knockout.js - Update observed model in custom knockoutjs binding -
i'm trying integrate jquery autocomplete extension in foreach knockout loop. i've created custom binding , autocomplete works. after autocomplete need update observable item model , cannot figure out how it. here code:
<!-- ko foreach: items --> <input data-bind="autocomplete: { source: '/products/item-search-ajax', options: { delay: 100, max: 20, minchars: 2, extraparams: { itcode: 1 }}, findcallback: $parent.findvaluecallback }, value: partnumber" type="text"> <!-- /ko --> <script type="text/javascript"> ko.bindinghandlers.autocomplete = { init: function (element, valueaccessor, allbindings, viewmodel, bindingcontext) { var settings = valueaccessor(); var source = settings.source; var options = settings.options; var findcallback = settings.findcallback; $(element).autocomplete(source, options); $(element).result(findcallback); ko.bindinghandlers.value.init(element, valueaccessor, allbindings); }, update: function(element, valueaccessor) { var value = valueaccessor(); ko.bindinghandlers.value.update(element,valueaccessor); } }; function quoteviewmodel() { var self = this; self.items = ko.observablearray(...); self.findvaluecallback = function(event, data, formatted) { // need update item model here autocomplete }; } </script>
you can pass $data
(which in foreach
item model) object function returns handler function such:
self.findvaluecallback = function(item) { return function(event, data, formatted) { //u have access particular item } }
and inside binding: findcallback: $parent.findvaluecallback($data)
Comments
Post a Comment