[英]Is there an Angular $scope event emitted after bindings have been applied to a controller?
[英]Update Firebase ref for KnockoutFire observable after bindings have been applied
我目前正在將KnockoutJS與一個具有我的站點所需的所有屬性的視圖模型一起使用。 當我一開始初始化viewmodel時,請確保為相關屬性分配null可觀察值,以便稍后對其進行更新時,將觸發事件。 我正在嘗試對淘汰賽集合執行相同的操作-特別是我想在用戶登錄后加載數據而不必重新應用綁定-但這樣做時感覺有點沉重。 這就是我所擁有的,並且是我實際上能夠在不稍后重新應用綁定的情況下使它工作的唯一方法:
// init view model
var viewModel = {
prop1: ko.observable(null),
firebase1: KnockoutFire.observable(recommenderFirebase.child('ratings/suggestions'), {
'.limit': 0,
"$suggestion" : {
accountID: true,
itemID: true,
relevance: true
}
});
}
// apply all bindings for life of app
ko.applyBindings(viewModel);
// login user and re-populate firebase1
firebaseOOO.initAuthClient(function(user) {
viewModel.firebase1 = KnockoutFire.observable(recommenderFirebase.child('ratings/suggestions').child(user.id), {
'.limit': 20,
"$suggestion" : {
accountID: true,
itemID: true,
relevance: true
}
});
});
是否有更好,更清潔,更有效的方法來做到這一點?
如何使用可觀察的包裝器,然后在登錄后替換可觀察的值?
首先,applyBindings viewModel如下所示:
var viewModel = {
prop1: ko.observable(null),
afterLogin: ko.observable(null)
};
然后登錄后,設置afterLogin
新結構:
viewModel.afterLogin({
firebase1: KnockoutFire.observable(…)
});
相應的視圖可能如下所示:
<!-- ko with: afterLogin -->
<ul data-bind="foreach: firebase1">
<li><span data-bind="text: accountID"></span></li>
</ul>
<!-- /ko -->
添加amplify js可以輕松跟蹤松散耦合的發布/訂閱事件。 登錄前定義(訂閱)事件,然后在登錄時發布事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.