jquery - Post data back to server using Knockout mapping -
i've bind list of objects check boxes list using knockoutjs , knockoutjs mapping plugin, code
server side class
public struct filterlistitem { public string text { get; set; } public string value { get; set; } }
javascript
$(document).ready(function () { var dto = { 'categoryid': geturlvars()["scid"] }; $.ajax({ url: "productlisttest.aspx/getfiltersweb", data: json.stringify(dto), type: "post", contenttype: "application/json", datatype: "json", timeout: 10000, success: function (result) { bindfiltermodel(result); } }); }); function bindfiltermodel(data) { console.log(data); var jsonobject; jsonobject = ko.mapping.fromjs(data); var viewmodel = { categorylist: jsonobject.d }; ko.applybindings(viewmodel); }
html
<div data-bind="foreach: categorylist.subcategorylist"> <div class="line"> <div> <input type="checkbox" data-bind="value: value" /><span data-bind="text: text"></span> </div> </div> </div>
now need post user selected data webmethod (with ajax call), when user selected checkbox, can not figure out how bind event trigger ajax call.
you can use rp's dirty plugin
viewmodel = function(data) { ko.mapping.fromjs(data, {}, this); this.dirty = ko.dirtyflag(this.items, false); this.dirty.isdirty.subscribe(this.ondirty, this); }; viewmodel.prototype = { ondirty: function() { console.log("post here"); this.dirty.reset(); } };
Comments
Post a Comment