I created a simple demo here that demonstrates the problem:
http://jsfiddle.net/boblauer/eCugY/
Basically, I want my userUpdated function to run when I update the user property, but it only runs once when the page loads.
Any help is appreciated.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your problem is that you expect the
userUpdatedcomputed to fire when the user is updated because you set the user as the computed’s context.How these the KO computed work is, it first fires immediately and in the course of firing any observables that are encountered are recorded. When one of those observables is updated the computed function fires again. In your case your function calls no observables. It simply calls alert. The context object is not counted among the observables list.
You could get this to work by just using the user observable in your function. Alternativly you probably should be using a subscribe which is a function that get’s called when an observable changes. Replace your computed with
Computed’s are usually used to return computed values. If you start to use them as arbitrary function callers you can end up getting into infinite loops (if you decide to update an observable within the computed).
Hope this helps.