gpt4 book ai didi

javascript - Ampersand JS 中的多个数据绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 07:43:23 26 4
gpt4 key购买 nike

我有一个模型:

var Person = AmpersandState.extend({
props: {
name: {
type: 'string',
default: ''
}
},
session: {
isSitting: {
type: 'boolean',
default: false
}
},
toggleState: function () {
this.toggle('isSitting');
}
});

View :

var PersonView = AmpersandView.extend({
template: Templates.person,
events: {
'change [type=checkbox]': 'handleCheckbox'
},
bindings: {
'model.name': {
type: 'text',
hook: 'name'
},
'model.isSitting': [
{
type: 'toggle',
hook: 'sitting'
},
{
type: 'toggle',
hook: 'standing'
}
]
},
handleCheckbox: function () {
this.model.toggleState();
}
});

和模板:

<li>
<div data-hook="name"></div>
<span data-hook="sitting">is sitting</span>
<span data-hook="standing">is standing</span>
<input type="checkbox">
</li>

因此,当复选框更改时,它会切换 isSitting模型上的属性以及 span 的可见性与 data-hook="sitting"相应地切换。但我想做的是切换 data-hook="standing" span也可以,但是切换它以使其执行相反的操作。

所以当 sitting跨度已隐藏,standing应显示跨度。无论如何,是否可以使用多个数据绑定(bind)来做到这一点?或者我是否必须显式定义另一个属性,例如 isStanding在模型和我的 toggleState 中方法,也切换该属性吗?

最佳答案

尝试使用切换绑定(bind)上的yesno键:

// show/hide where true/false show different things

'model.isSitting': {
type: 'toggle',
yes: '[data-hook=sitting]',
no: '[data-hook=standing]'
}

https://github.com/AmpersandJS/ampersand-dom-bindings#toggle 上所示

关于javascript - Ampersand JS 中的多个数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35313712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com