javascript - How Do I Call A Function from ViewModel inside bindingHandler -


I have a method that calls my visual model, from which I get an AJAX method to get all the items It happens. It all works right. Then I have one. The banding handler is used to inline the images through the Ajax service. It also works, but I have to duplicate this issue, which receives all the items of the matching handler, which uploads the images, or the image is used after uploading the image. Refreshing the page can solve this problem but I have to refresh the scene with the new image. So the real question is that it is possible to call the function under a binding handler from a module? Any help would be appreciated!

Binding Handle:

  ko.bindingHandlers.imageInLineUpload = {update: function (element, value, usage, all binding, often, see modell, binding content ) {Var options = ko.utils.unwrapObservable (valueAccessor ()), property = ko.utils.unwrapObservable (options.property); $ (Element) .change (function () {if (element.files.length) {if ((element.files [0] .type === "image / png") (element.files [0] type == = "Image / jpeg")) {var $ this = $ (this); $ (element.form) .ajaxSubmit ({url: $ .API.url + "XYZ", type: "post", data type: " Text ", success: work (data) {toastr.success ('upload successful.'); Var viewmodel = new MyViewModel (); visualmodel.getObjects ();}, error: function (jqXHR, textstats, errorstroke) {Toastr. Error ('upload failed, please try again!');}});} Else {toastr.error ('Upload failed. PNG and JPEG are supported formats only.');}}}); }};  

View mode:

  var MyViewModel = function () {var self = this; Self.xyz = ko.observable (""); Self.xyz = ko.observable (""); Self.xyz = ko.observable (""); Self.GetObjects = function () {// Call to bring Ajax object} Self.GetObjects (); }; Ko.applyBindings (new MyViewModel ());  

There are two ways to do this: To really call a View MODEL function from "BindHandler" Way ":

  var viewModel = ko.dataFor (element); ViewModel.GetObjects ()  

This will work, but this is a simple middle solution; This connects its binding handler to this specific visual model in a very specific way and it is an inherent dependency; The Binding Handler API does not clarify that this function is required.


Another way (in my opinion, a better way) is to pass the function as a choice argument in the binding handler:

  & lt; Div data-bound = "imageInLineUpload: {getImages callback: GetObjects}" & gt; & Lt; / Div & gt;  

In bindhandler:

  var option = ko.utils.unwrapObservable (valueAccessor ()), getImages callback = options.getImagesClall; GetImagesCallback (); (This is similar to the answer to PW Cuddle, but I think it is a bad idea to specify a different binding key to provide alternatives to a different binding hander, and this is a very ugly way 


But in fact, if your binding handle is calling asynchronous call, then I think that you need to do some reconstruction in my opinion, only binding hands The argument must be done, which is not in dealing with AJAX, which should be in the view modal.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -