knockout.js - knockout cant process click binding as not a function -


i struggling calling function knockout!

var bankviewmodel = function () {     self.addbank = function(){                 return function(){                     self.addshow(true);                     var bank = new observablebank('',"","","","","","","","","active");                     self.newbank(bank);                 };         }; };  var bankviewmodelinstance = new bankviewmodel(); ko.applybindings(bankviewmodelinstance, document.getelementbyid("company-info-bank")); 

and in view have tried loads of variations of binding with:-

<button id="demo-btn-addrow" class="btn btn-purple btn-labeled fa fa-plus" data-bind="click: addbank()">add new</button> 

tried $parent (undefined), $data - nothing etc.

can tell me silly mistake making?

thanks

tough code works check here .

couple of corrections/improvements :

  • you can use data-bind="click: addbank" no need bind addbank() inside click function make click fire onload .
  • click function logic in viewmodel don't need return function()

viewmodel:

var bankviewmodel = function () {     var self = this;     self.newbank = ko.observablearray();     self.addshow = ko.observable();     self.addbank = function () {         self.addshow(true);         /*                    var bank = new observablebank('',"","","","","","","","","active"); */         self.newbank.push(1); //for testing      }; };  var bankviewmodelinstance = new bankviewmodel(); ko.applybindings(bankviewmodelinstance); 

sample working fiddler here


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -