访问视图模型的属性 [英] Accessing viewModel properties

查看:111
本文介绍了访问视图模型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板绑定如下:

I have a template binding as follows

<tbody id ="mytemplatetbody"
       data-bind="template: {name: 'myTemplate', foreach: Items}">
</tbody>

在模板的行有一个状态字段的值可以是1或2或3。有此屏幕上的三个复选框,并根据哪些复选框(ES)用户选择,行应该是可见的。

The rows in template have a status field whose value can be 1 or 2 or 3. There are three checkboxes on this screen and depending on what checkbox(es) user selects, the rows should be visible.

这是我做了什么:增加了三种可观测性能视图模型,并将其绑在三个复选框。我可以按如下方式显示这些值:

This is what I have done: Added three observable properties to viewModel and tied them to the three checkboxes. I can display those values as follows:

<span data-bind="text: viewModel.checkBox1Selected()"></span>

问:我不能把任何如果我例如像这样的报表模板...

Question: I am not able to put any if statements in my template for example like this...

{{if viewModel.checkBos1Selected() }} 

...所以什么是最好的方式还是反正我可以做到什么上述我?

...so what is the best way or anyway I can accomplish what I described above?

推荐答案

做这类事情的常用方法是创建一个dependentObservable重新present你过滤行。然后,绑定你反对这个dependentObservable模板。每当你观察到的过滤器的变化之一,那么过滤的行会被重新评估。

A common way to do this type of thing is to create a dependentObservable to represent your filtered rows. Then, bind your template against this dependentObservable. Whenever one of your observable filters changes, then the filtered rows will be re-evaluated.

下面是一个例子: http://jsfiddle.net/rniemeyer/BXeCb/

您当然可以做但是过滤最适合您的应用程序。我用了核对的observableArray结合握住我的过滤器,但你可以轻松与您的视图模型使用三个观察的性能的方法去。

You can certainly do the filtering however works best for your app. I used the checked binding against an observableArray to hold my filters, but you could easily go with your method of using three observable properties on your view model.

这篇关于访问视图模型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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