0

I am working on KnockoutJS with jQuery. I need to apply custom binding say myBinding on multiple views.

I have a case where multiple views are bound with myBinding, a JS method need to be called which will pass some viewmodel data from these views.

I don't have control on how many templates will be there on a page. So I am looking for some handle which keeps track of all the templates on page which use myBinding. Once all the views on page are loaded, the json passed along with the myBinding would be sent in an AJAX request.

HTML templates:

<div id="myDiv1" data-bind="myBinding:myJson"> </div>
<div id="myDiv2" data-bind="myBinding:myJson"> </div>
<div id="myDiv3" data-bind="myBinding:myJson"> </div>
<div id="myDiv4" data-bind="myBinding:myJson"> </div>

JS Code:

ko.bindingHandlers.myBinding = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext){
    },
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext){
    }
};

Once all these bindings are called I want to call following update function:

//JS Code
function updateBindingTemplates{
    // do something
}

Is there any way to achieve the same thing?

Thanks.

harris
  • 47
  • 6
  • There are many ways to do it. However it isn't the way I'd do it, because ko isn't expected to work like that, and you'll probaby get into trouble. Apart from explaining what you want to do with code, can you explain why you need to do it that way? Is there a special reason to do it in that way? – JotaBe Feb 02 '16 at 11:45
  • I need to get the views registered with my module so that once all the views are loaded I can post that data in a single AJAX request. Can you suggest the ways to do it? – harris Feb 02 '16 at 14:38
  • I still cannot understand what you need to do. It could smell of components and promises, but you must give a better explanation. And, please, when you're are asked to give more information in a comment, improve your question instead of answering with another comment, so that it gets visible to all other people. – JotaBe Feb 02 '16 at 16:22

0 Answers0