- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Javascript 新手(从今天开始),我正在使用 Ractive 框架制作一个 Web 应用程序来提供分析产品。我正在尝试创建一个函数来翻转 .on 函数中的 bool 值。我有类似的东西,但它不起作用。有人可以帮我思考一下这个问题吗?
ractive.on('flipBool', function ( ) {
ractive.set( 'theData.*.Visible', !'theData.*.Visible' );
});
最佳答案
根据 ofrommel 的回答,我想我应该快速解释初始代码片段中发生的情况,因为它将来可能会有所帮助。
当您调用 ractive.set('theData.*.Visible', !'theData.*.Visible')
时,您正在设置与 theData.*.Visible 匹配的所有内容
为单个值,即 !'theData.*.Visible
- 并且因为 !
运算符简单地否定其后面的任何内容,以及一个非空字符串被认为是真的,!'theData.*.Visible' === false
。所以这相当于这样做:
ractive.set( 'theData.*.Visible', false );
因此,您必须实际获取键路径的值,而不是在第二个参数中使用键路径:
// 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
,这不是您想要的。 (我已经 just opened an issue on GitHub 来解决这个问题。)
因此,目前实现您想要的效果的最佳方法是迭代数组并手动更新所有内容,如下所示:
ractive = new Ractive({
el: 'main',
template: '#template',
data: {
people: [
{ name: 'Alice', visible: false },
{ name: 'Bob', visible: true },
{ name: 'Caroline', visible: true },
{ name: 'Dave', visible: false },
{ name: 'Eric', visible: false }
]
},
flipBool: function () {
var changes = {};
this.get( 'people' ).forEach( function ( person, i ) {
changes[ 'people.' + i + '.visible' ] = !person.visible;
});
this.set( changes );
}
});
<script src="http://cdn.ractivejs.org/latest/ractive.js"></script>
<main></main>
<script id='template' type='text/html'>
<button on-click='flipBool()'>flip</button>
{{#each people}}
{{#if visible}}
<p>{{name}} is visible</p>
{{/if}}
{{/each}}
</script>
关于javascript - Ractive.js 翻转 bool 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27626357/
我正在使用https://github.com/Nijikokun/ractive.sortable.js向表格添加可拖动排序的功能。 var resources = [ {
我正在将应用程序从 Ractive JS v0.5.8 升级到 v0.7.1,在浏览器控制台中,我收到 403(禁止)请求调用: http://[mydomain.com]/www/RACTIVE/r
如果我的 Ractive 模板中有以下内容: click me 然后我可以用这个来监听点击: app.on({ handleClick:function() { alert("
除了调用 init 之外,我不确定 Ractive 组件的生命周期是什么。例如,什么时候直接访问 DOM 是安全的?我尝试使用流行的迷你图 jQuery 插件,但我从未让它工作。我使用了一个指令 -
我正在尝试一个非常简单的 conditional expression在一个活跃的模板中。 参见the jsfiddle - 这是我的数据: {name: 'steve'} 这是我的模板: value
我想知道 Ractive 生成的 DOM 元素何时准备就绪。就我而言,我想使用 jquery 将自动完成功能附加到元素上。理想情况下它会是这样的: 模板: {{#list}} {{/list}}
我正在使用 jQuery 和 RactiveJS 模板开发一个独立的小部件。最初,小部件使用动态输入表单元素打开,并且这些元素通过 AJAX 调用填充。这里的电子邮件是一个静态字段。这是用户的初始 V
假设我有两个模板:一个 index带有我的主要内容的模板,还有一个 changelog带有变更日志的模板。它们被认为是不同的模板: // ... // ... ractive = new Ra
我想观察 docs on ractive observe 的关键路径的变化。但是,我希望能够同时观察多个路径,例如: var binding = new Ractive({ el: '.here'
我一直在尝试使用组件加载器,如 component spec 中所述。 。我不确定如何通过组件文件执行类似的操作: var bookmarkableBaseClass = Ractive.extend
这很像网络上常见的多个复选框问题,只是我的复选框列表是动态的。 简化: {{#user}} Roles {{#allRoles:roleIndex}} {{.name}}
我在 Ractive 中渲染了一个选择多个,并计算出所有可能选项的列表,效果非常好。但我找不到预选值的正确方法。 到目前为止,我有类似的东西: data = [{ type: "Person",
我有一个正在迭代的列表: {{#list}} {{this.value}} {{/list}} 如果 {{this.hidden}} 为 true,我将如何继续循环? 我想
使用 Ractive,我想生成一个包含从 1 到 n 的数字选项的下拉列表。 可以使用 ( source ) 生成选择元素: Select a country {{#countrie
假设我在页面上有一个根 Ractive,以及在假设的主干路由器导航到路由时显示的各种小部件: var widget1=Ractive.extend({template:"{{foo}}"}); var
基本上我想要这个:In Mustache templating is there an elegant way of expressing a comma separated list without
我发现自己处于一种情况,我想从父 Ractive 访问从 Ractive 组件创建的实例。 例如。 var myComponent = Ractive.extend( { us
我正在尝试获取已选中复选框的值并将其存储在数组中。每当用户单击任何复选框时,我想仅使用已选中复选框的值更新数组。我该怎么做? {{#if checked}}checked!{{else}}
我想从 ractive 调用特定对象的方法。 on-click="mylist.page(5)" 在这种情况下,具有 next 方法的 mylist 对象将设置在 ractive 对象上。 当我使用直
嘿,我已经学习了一点 RactiveJS,到目前为止我真的很喜欢它。我想将它与 RequireJS 一起使用,发现了这个: https://github.com/Rich-Harris/Ractive
我是一名优秀的程序员,十分优秀!