0

I got two different view models and I need to call a function in one view model from other. To acheive this i try to use ko.subscribable()

 var postbox=new ko.subscribable();// global variable 

EmployeeViewModel

  postbox.subscribe(function (showAlert) {
            return that.ValidateEmployees(showAlert);
        }, that, 'ValidateEmps');

  that.ValidateEmployees=function(){

  //validation logic
   return true/false;
  }

TaxCalculatorViewModel

var isEmpValid = postbox.notifySubscribers(true, "ValidateEmps");
 if (!isEmpValid ) {
  return false;
 }

The prioblem is I am always getting isEmpValid=undefined instead of getting the return value of ValidateEmployees

How to get the return value of a function when we pass using notifySubscribers?

Billa
  • 5,226
  • 23
  • 61
  • 105
  • @Quentin, is it applicable to `ko` knockout? So calling like `postbox.notifySubscribers(true, "ValidateEmps")()` will resolve? – Billa Mar 03 '15 at 09:36
  • KO subscriptions are "one way". So the `notifySubscribers` just sends out the message and in the subscribe you read that message and that is it there is no way to return from an event handler. If you need "two way" commutation then you need to need events: http://jsfiddle.net/h0j773xg/ – nemesv Mar 03 '15 at 10:51
  • @nemesv, Thank you veru much!. Is there any example to acheive this with events? I am trying to call another viewmodel method and expecting the return results. – Billa Mar 03 '15 at 11:49

0 Answers0