淘汰赛订阅可观察对象 [英] Knockout subscribe to observable object

查看:63
本文介绍了淘汰赛订阅可观察对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象:

{ model: settings: { "FirstName": "Joe", "LastName": "Bloggs" } };

在我的视图模型中,可以使设置可见:

in my view model I make settings an observable:

this.Settings = ko.observable(ko.mapping.fromJS(model.settings));

这使得FirstNameLastName也是可观察的.

Which makes FirstName and LastName observables too.

我认为我是这样做的:

<p data-bind="text: Settings().FirstName"></p>

要从Settings中读取值,我要做的是:

to read a value from Settings I do:

`this.Settings().FirstName()`

FirstNameLastName更改时我想订阅时,就会出现问题.

The problem comes in when I want to subscribe when FirstName OR LastName changes.

我知道我可以做this.Settings.FirstName.subscribe...,但是一旦您获得了两个以上的观察结果,那就很痛苦了.

I know I can do this.Settings.FirstName.subscribe... but that's painful once you get more than two things you're observing.

有没有办法做this.Settings.subscribe...?还是应该使用自定义绑定?

Is there a way to do this.Settings.subscribe...? or should I be using a custom binding?

推荐答案

我在下面编写了插件来帮助我处理此类情况:

I wrote the plugin below to help me deal with such situations:

https://github.com/ZiadJ/knockoutjs-reactor

这是一个简单的用法示例:

Here's a simple usage example:

 this.Settings.watch(function(root, trigger){
     var newValue = trigger();
 });

这篇关于淘汰赛订阅可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆