结合带缺口的锯齿状阵列 [英] Binding Jagged Array with knockout

查看:100
本文介绍了结合带缺口的锯齿状阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在绑定敲除js中的锯齿数组时遇到问题.我已经搜索了很多,但是找不到任何东西.让我们举一个简单的例子,

I have having problem to binding jagged array in knockout-js. I have search a lot but could not found any thing. Let make a simple example,

<div data-bind="foreach: items">
<div  data-bind="style: { textAlign: align, backgroundColor:  bgColor, fontFamily: fontFamily, fontSize: size, color: color }, text: title"></div>
</div>

这是我的数组,

var items = [{
                    title: 'A',
                    align: 'right',
                    fontFamily: 'helvetica',
                    color: '#777777',
                    bgColor: '#ffffff'
            }, 
            {
                    title: 'A',
                    align: 'right',
                    size: 'large',
                    fontFamily: 'helvetica'
            }
            {
                    size: 'large',
                    fontFamily: 'helvetica',
                    color: '#777777'
            }]

很明显有时缺少某些财产吗?所以我得到 * 未定义*错误.如何处理这种情况.

Clearly sometime some property(ies) are missing? So I get * is not defined* error. How to handle this situation.

推荐答案

您有两个选择.您可以确保您的ViewModel不会锯齿.通过对模型使用敲除映射插件并使其使用您的类型的构造函数,可以很好地实现此目的.这样,您甚至可以定义默认值

you have two options. You can make sure your ViewModels aren't jagged. This is accomplished well by using the knockout mapping plugin against your model and having it use a constructor of your type. this way you can even define defaults

另一个选择是引用$ data上下文中的值. IE浏览器:

the other option is to reference the values off of the $data context. IE:

<div  data-bind="style: { textAlign: $data.align, backgroundColor:  $data.bgColor, fontFamily: $data.fontFamily, fontSize: $data.size, color: $data.color }, text: $data.title"></div>

小提琴: http://jsfiddle.net/drdamour/Xp9Er/

这篇关于结合带缺口的锯齿状阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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