在 RactiveJS 上更新数组
Updating an array on RactiveJS
我正在使用 RactiveJS,但在使用数组时遇到问题,这是我的初始配置:
JSFIDDLE:http://jsfiddle.net/18fccpnj/
var ractive = new Ractive({
el: 'container',
template: '#template',
// Here, we're passing in some initial data
data: { students: [
{name: "Miguel Crespo", note: 5}
]}
});
我的 html 上有一个触发事件的按钮
<button on-click="charge">Cargar otro</button>
这是事件句柄:
ractive.on("charge", function(){
ractive.set("students", [
{name: "Miguel Crespo", note: 5},
{name: "Pedro Perez", note: 3}
]);
});
此调用从不更新视图,我不知道为什么,因为在官方页面中,这样的示例有效!
感谢您的帮助。
为块使用静态定界符 [[#each students:num]]
声明它不会在初始呈现后更新。将它们更改为 {{#each students:num}}
,它工作正常(参见 http://jsfiddle.net/18fccpnj/4/)。
也不需要重新设置整个数组的数据,直接push新成员即可:
ractive.push("students", {name: "Pedro Perez", note: 3});
我正在使用 RactiveJS,但在使用数组时遇到问题,这是我的初始配置:
JSFIDDLE:http://jsfiddle.net/18fccpnj/
var ractive = new Ractive({
el: 'container',
template: '#template',
// Here, we're passing in some initial data
data: { students: [
{name: "Miguel Crespo", note: 5}
]}
});
我的 html 上有一个触发事件的按钮
<button on-click="charge">Cargar otro</button>
这是事件句柄:
ractive.on("charge", function(){
ractive.set("students", [
{name: "Miguel Crespo", note: 5},
{name: "Pedro Perez", note: 3}
]);
});
此调用从不更新视图,我不知道为什么,因为在官方页面中,这样的示例有效!
感谢您的帮助。
为块使用静态定界符 [[#each students:num]]
声明它不会在初始呈现后更新。将它们更改为 {{#each students:num}}
,它工作正常(参见 http://jsfiddle.net/18fccpnj/4/)。
也不需要重新设置整个数组的数据,直接push新成员即可:
ractive.push("students", {name: "Pedro Perez", note: 3});