Javascript Ractive.js翻转布尔函数

Javascript Ractive.js翻转布尔函数,javascript,ractivejs,Javascript,Ractivejs,我是Javascript新手(从今天开始),我正在使用Ractive框架制作一个web应用程序来交付分析产品。我正在尝试创建一个函数,该函数翻转.on函数中的布尔值。我有类似的东西,但不起作用。有人能帮我思考一下这个问题吗 ractive.on('flipBool', function ( ) { ractive.set( 'theData.*.Visible', !'theData.*.Visible' ); }); 为什么不使用Ractivetoggle()函数?为什么不使用Racti

我是Javascript新手(从今天开始),我正在使用Ractive框架制作一个web应用程序来交付分析产品。我正在尝试创建一个函数,该函数翻转.on函数中的布尔值。我有类似的东西,但不起作用。有人能帮我思考一下这个问题吗

ractive.on('flipBool', function ( ) {
  ractive.set( 'theData.*.Visible', !'theData.*.Visible' );
});

为什么不使用Ractive
toggle()
函数?

为什么不使用Ractive
toggle()
函数?

为什么不使用Ractive
toggle()
函数?

根据Rommel的回答,我想我应该很快解释一下最初的代码片段中发生了什么,因为这可能会在将来有所帮助

调用
ractive.set('theData.*.Visible',!'theData.*.Visible')
时,您正在将与
theData.*.Visible
匹配的所有内容设置为单个值,即
!'数据。*.可见
-因为
运算符只对其后的任何内容求反,非空字符串被认为是真实的,
!'数据。*.Visible'==false
。这相当于这样做:

ractive.set( 'theData.*.Visible', false );
因此,与在第二个参数中使用keypath不同,您必须实际获取keypath的值:

// this...
ractive.toggle( 'foo' );

// ...is equivalent to this:
ractive.set( 'foo', !ractive.get( 'foo' ) );
不幸的是,这实际上不适用于包含
*
字符的关键路径:

// this...
ractive.toggle( 'theData.*.Visible' );

// ...is equivalent to this...
ractive.set( 'theData.*.Visible', !ractive.get( 'theData.*.Visible' ) );

// ...which is equivalent to this:
 ractive.set( 'theData.*.Visible', true );
因为
ractive.get('theData.*.Visible')
始终是
未定义的
,这意味着切换该值将始终将所有匹配的键路径设置为
true
,这不是您想要的。(我必须解决这个问题。)

因此,目前实现所需的最佳方法是迭代数组并手动更新所有内容,如下所示:

ractive=新的ractive({
el:'主要',
模板:“#模板”,
数据:{
人民:[
{name:'Alice',可见:false},
{name:'Bob',visible:true},
{name:'Caroline',可见:true},
{name:'Dave',visible:false},
{name:'Eric',可见:false}
]
},
flipBool:函数(){
var变化={};
this.get('people').forEach(函数(person,i){
更改['people.+i+'.visible']=!person.visible;
});
这个。设置(更改);
}
});

轻弹
{{{每个人}
{{#如果可见}
{{name}}可见

{{/if} {{/每个}}
根据ofrommel的回答,我想我应该快速解释一下最初的代码片段中发生了什么,因为这可能会对将来有所帮助

调用
ractive.set('theData.*.Visible',!'theData.*.Visible')
时,您正在将与
theData.*.Visible
匹配的所有内容设置为单个值,即
!'数据。*.可见
-因为
运算符只对其后的任何内容求反,非空字符串被认为是真实的,
!'数据。*.Visible'==false
。这相当于这样做:

ractive.set( 'theData.*.Visible', false );
因此,与在第二个参数中使用keypath不同,您必须实际获取keypath的值:

// this...
ractive.toggle( 'foo' );

// ...is equivalent to this:
ractive.set( 'foo', !ractive.get( 'foo' ) );
不幸的是,这实际上不适用于包含
*
字符的关键路径:

// this...
ractive.toggle( 'theData.*.Visible' );

// ...is equivalent to this...
ractive.set( 'theData.*.Visible', !ractive.get( 'theData.*.Visible' ) );

// ...which is equivalent to this:
 ractive.set( 'theData.*.Visible', true );
因为
ractive.get('theData.*.Visible')
始终是
未定义的
,这意味着切换该值将始终将所有匹配的键路径设置为
true
,这不是您想要的。(我必须解决这个问题。)

因此,目前实现所需的最佳方法是迭代数组并手动更新所有内容,如下所示:

ractive=新的ractive({
el:'主要',
模板:“#模板”,
数据:{
人民:[
{name:'Alice',可见:false},
{name:'Bob',visible:true},
{name:'Caroline',可见:true},
{name:'Dave',visible:false},
{name:'Eric',可见:false}
]
},
flipBool:函数(){
var变化={};
this.get('people').forEach(函数(person,i){
更改['people.+i+'.visible']=!person.visible;
});
这个。设置(更改);
}
});

轻弹
{{{每个人}
{{#如果可见}
{{name}}可见

{{/if} {{/每个}}
根据ofrommel的回答,我想我应该快速解释一下最初的代码片段中发生了什么,因为这可能会对将来有所帮助

调用
ractive.set('theData.*.Visible',!'theData.*.Visible')
时,您正在将与
theData.*.Visible
匹配的所有内容设置为单个值,即
!'数据。*.可见
-因为
运算符只对其后的任何内容求反,非空字符串被认为是真实的,
!'数据。*.Visible'==false
。这相当于这样做:

ractive.set( 'theData.*.Visible', false );
因此,与在第二个参数中使用keypath不同,您必须实际获取keypath的值:

// this...
ractive.toggle( 'foo' );

// ...is equivalent to this:
ractive.set( 'foo', !ractive.get( 'foo' ) );
不幸的是,这实际上不适用于包含
*
字符的关键路径:

// this...
ractive.toggle( 'theData.*.Visible' );

// ...is equivalent to this...
ractive.set( 'theData.*.Visible', !ractive.get( 'theData.*.Visible' ) );

// ...which is equivalent to this:
 ractive.set( 'theData.*.Visible', true );
因为
ractive.get('theData.*.Visible')
始终是
未定义的
,这意味着切换该值将始终将所有匹配的键路径设置为
true
,这不是您想要的。(我必须解决这个问题。)

因此,目前实现所需的最佳方法是迭代数组并手动更新所有内容,如下所示:

ractive=新的ractive({
el:'主要',
模板:“#模板”,
数据:{
人民:[
{name:'Alice',可见:false},
{name:'Bob',visible:true},
{name:'Caroline',可见:true},
{name:'Dave',visible:false},
{name:'Eric',可见:false}
]
},
flipBool:函数(){
var变化={};
this.get('people').forEach(函数(person,i){
更改['people.+i+'.visible']=!person.visible;
});
这个。设置(更改);
}
});

轻弹
{{{每个人}
{{#如果是