Sunday, 15 August 2010

knockout.js - How to get binded observable content inside a click function of a button? -


Well I was facing a difficult case here but to simplify things for everyone's understanding Let me explain your scenario.

I have a button with the related bindings, with two buttons like text: name1 and text: name2 click binding.

ViewModel:

  var viewModel = function () {var self = this; Self.arr = ko.observableArray ([1]); Self.name1 = ko.observable ("click1"); Self.name2 = ko.observable ("click2"); Self.clickme = function () {console.log (this) // this.name1 ('yoo') should be dynamic based on this.name1 or name2 btn click // clicked button}} ko.applyBindings (new viewmodel ());  

When a button is clicked, I have no way to get the clicked button text binded viewable inside the click function i.e. name1 inside my function if clicked by button1

I tried $ element , the value of the VM as the $ context parameter Passing it to function does not resolve it.

Demo Bella

Any help or ideas on this would be good. Simply pass them in the

function.

  & lt; Div data-bind = "foreach: arr" & gt; & Lt; Button data-bind = "text: $ root.Nname1, click: function () {$ root.clickme ($ root.name1)}" & gt; & Lt; / Button & gt; & Lt; Button data-bind = "text: $ root.Nname2, click: function () {$ root.clickme ($ root.name2)}" & gt; & Lt; / Button & gt; & Lt; / Div & gt;  

Then handle them in your visual model like this:

  self.clickme = function (val) {warning (val ()); }  

here is a


No comments:

Post a Comment