i'm trying use knockout view i'm uploading documents , showing list. i'm using jquery.form.js in order upload them using ajax. i've changed use knockout , viewmodel looks this
var viewmodel = function (groups) { var self = this; self.groups = ko.observablearray(ko.utils.arraymap(groups, function (group) { return { planname: ko.observable(group.key), documentlist: ko.observablearray(ko.utils.arraymap(group.values, function (value) { return { document: ko.observable(new document(value)) }; })) }; })); var options = { datatype: 'json', success: submissionsuccess }; self.add = function () { $('#addform').ajaxsubmit(options); return false; }; function submissionsuccess(result) { alert('success'); } }; having 1 document function doing mapping. i'm stuck when receiving json data controller. result correct, list of objects in same format i'm receiving on first load don't know how "refresh" viewmodel use new list.
don't know if using ko mapping plugin make easier have never used , don't know if it's applicable this.
the controller method, in case relevant, (if else neede let me know althoug won't have access code in next hours)
[httppost] public actionresult adddocument(adddocumentviewmodel viewmodel) { var partyid = utils.getsessionpartyid(); if (viewmodel.file.contentlength > utils.getkbmaxfilesize * 1024) modelstate.addmodelerror("file", string.format("the file exceeds limit of {0} kb", utils.getkbmaxfilesize)); if (modelstate.isvalid) { _documentsmanager.adddocument(viewmodel, partyid); if (request.isajaxrequest()) { var vm = _displaybuilder.build(partyid); return json(vm.documents); } return redirecttoaction("index"); } var newviewmodel = _createbuilder.rebuild(viewmodel, partyid); return partialview("_adddocument", newviewmodel); } thanks
edit: came code seems work (this function inside viewmodel one
function submissionsuccess(result) { self.groups(ko.utils.arraymap(result, function (group) { return { planname: ko.observable(group.key), documentlist: ko.utils.arraymap(group.values, function (value) { return { document: new document(value) }; }) }; })); };
are sure documentlist , document need observables ?
to update list can push you'd on regular array. try this:
function submissionsuccess(result) { self.groups.removeall(); $.each(result, function(index, value) { var documentlist = []; $.each(value.values, function(index, value) { documentlist.push(new document(value)); }); var group = { planname:value.key, documentlist: documentlist }; self.groups.push(group); }); };
Comments
Post a Comment