Is there is any way to find which property is calling the subscribed function, when we subscribe one function to more then one property ?
code example.
var self = this;
$(document).ready(function(){
var myViewModel =
{
FirstName : ko.observable("Bert"),
LastName : ko.observable("pual")
};
myViewModel.FirstName.subscribe(self.notifyChange);
myViewModel.LastName.subscribe(self.notifyChange);
ko.applyBindings(myViewModel);
});
notifyChange = function ( newValue ) {// want to find which property(FirstName/LastName) is calling the function }
HTML
<body>
Enter your name:
<input id="source" data-bind="value: FirstName" />
<input id="Text1" data-bind="value: LastName" />
here i subscribed the “notifyChange” function for both FirstName and LastName. if any one value is changed means,it will call the notifyChange function, i want to know which property changes make the notifyChange function call ?
You can’t tell who actually called the function.
One choice would be to use your
notifyChangefunction, butbindit in each case to the appropriate property. Now,thiswill be set to either theFirstNameorLastNameobservable. Doing abinddoes create a wrapper to the function, but at least your actual implementation innotifyChangewill only exist once.So,
thisinside ofnotifyChangewill be the appropriate observable.