BalaKrishnan웃
BalaKrishnan웃

Reputation: 4557

How to find which property is calling the subscribed method in Knockout?

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 ?

Upvotes: 4

Views: 1181

Answers (2)

Karim Ayachi
Karim Ayachi

Reputation: 717

I realize that this question was asked and answered over 6 years ago. But maybe my answer can still be useful for someone.

The accepted answer is based on passing the observable as the context for the callback function. By the way, you don't have to use bind(), you can pass the context as the second parameter to subscribe():

myViewModel.FirstName.subscribe(self.notifyChange, myViewModel.FirstName);

Knockout uses bind() internally.

But, if you (like I did) can't afford to change the context within your callback function (I need properties and functions within that context), why not simply wrap your callback function in another function and pass an extra parameter to the callback function:

myViewModel.FirstName.subscribe(function (value) { self.notifyChange('FirstName', value); });
myViewModel.LastName.subscribe(function (value) { self.notifyChange('LastName', value); });

If you use this in a loop, say if you're enumerating properties of your ViewModel, you can wrap the whole thing in an IIFE like this:

// .. property is an enumerated reference to an ko.observer
// .. propertyName is a string identifying it (for instance 'FirstName')
(function (elementName) {
    property.subscribe(function(value) { 
        self.notifyChange(elementName, value);
    });
})(propertyName);

Upvotes: 0

RP Niemeyer
RP Niemeyer

Reputation: 114792

You can't tell who actually called the function.

One choice would be to use your notifyChange function, but bind it in each case to the appropriate property. Now, this will be set to either the FirstName or LastName observable. Doing a bind does create a wrapper to the function, but at least your actual implementation in notifyChange will only exist once.

myViewModel.FirstName.subscribe(self.notifyChange.bind(myViewModel.FirstName));
myViewModel.LastName.subscribe(self.notifyChange.bind(myViewModel.LastName));

So, this inside of notifyChange will be the appropriate observable.

Upvotes: 2

Related Questions