knockout.js - knockout choose viewmodel depend on attribute -
i have that:
html:
<div data-bind="??? with?" id="mainitem"> <input type="text" id="mainquantity" value="12" data-bind="text: quantity"> шт <input type="button" value="buy" data-bind="click: cartadd" data-good-id="2134" /> </div>
script:
var mainviewmodel = function () { var self = this; var items = [ { id:2134, quantity: 12 }, .. ]; // data json self.cartadd = function (data, event) { var id = $(event.target).attr("data-good-id"); $.grep(self.items, function (i) { return i.id == id }). each(function (i) { i.quantity++; }) } }
so, need set viewmodel #mainitem element, viewmodel should items array, selected id, placed in "data-good-id" attribute.
my end result should be, quantity property binded #mainquantity element
thank you.
i think don't want use with binding. instead can foreach loop on items , access access cartadd function on parent context. example:
<div data-bind="foreach: items"> <input type="text" id="mainquantity" value="12" data-bind="text: quantity"> <input type="button" value="buy" data-bind="click: $parent.cartadd" data-good-id="2134" /> </div>
excuse typo on phone. hope helps.
Comments
Post a Comment