ここで、問題を示す簡単なデモを作成しました。
http://jsfiddle.net/boblauer/eCugY/
基本的に、userプロパティを更新するときにuserUpdated関数を実行したいのですが、ページが読み込まれるときに1回だけ実行されます。
どんな助けでも大歓迎です。
ここで、問題を示す簡単なデモを作成しました。
http://jsfiddle.net/boblauer/eCugY/
基本的に、userプロパティを更新するときにuserUpdated関数を実行したいのですが、ページが読み込まれるときに1回だけ実行されます。
どんな助けでも大歓迎です。
問題はuserUpdated
、ユーザーを計算対象のコンテキストとして設定しているため、ユーザーが更新されたときに計算対象が起動することを期待していることです。
これらのKO計算作業がどのようになっているのか、最初にすぐに発火し、発火の過程で遭遇した観測量が記録されます。これらのオブザーバブルの1つが更新されると、計算された関数が再度起動します。あなたの場合、あなたの関数はオブザーバブルを呼び出しません。単にアラートを呼び出します。コンテキストオブジェクトは、オブザーバブルリストにはカウントされません。
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
user.name.subscribe(function(newvalue) {
alert('user updated to ' + newvalue);
});
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.