knockout.js - Knockout observable only updates once inside jquery form -
i'm using jquery upload file client server. then, i'm using knockout filename , information file that's on server. however, i'm having 2 problems:
- the knockout observable "filename" gets updated once, first time file selected.
- the file name not being displayed.
i know has interaction of knockout , jquery, haven't been able figure out what. i'm using knockout 2.2.1, jquery 1.9.2, , file upload here. haven't been able replicate jsfiddle yet; when do, i'll update question.
html:
<script type="text/jscript"> jquery(function ($) { 'use strict'; // initialize jquery file upload widget: $('#fileupload').fileupload(); }); </script> <div id="fileupload"> <form action="fileuploadhandler.ashx" method="post" enctype="multipart/form-data"> <input type="file" name="files[]" data-bind="value: filename"/> </form> </div> <input type="button" value="upload" data-bind="click: uploadfile">
ko:
self.filename = ko.observable(""); self.uploadfile = function () { if (self.filename() == "") { alert('please choose file upload.'); } else { promiseload = callload(); $.when( promiseload ) .done(function () { showhide('loadingpanel', false, true); }); } }; self.callload = function () { return $.deferred(function (dfd) { service.callservicemethod("gettempdata", json.stringify({ filename: self.filename() }), function (msg) { if (msg.d.alert) { alert("success"); }; dfd.resolve(); }); }).promise(); };
note: actual uploading happening via fileuploadhandler.ashx, , not have same problems knockout (files selected after first uploaded correctly, not update filename).
both problems can fixed setting replacefileinput in jquery file upload false, rather true. when true, value cloned , file name not display; when false, value stays (letting bound ko) , displays normally. (thanks commenters putting me on right track.)
Comments
Post a Comment