jsviews 反应式助手不更新 depends = [...] 定义
jsviews reactive helper doesn't update with depends = [...] defined
我指出的一个助手依赖于我模型中的一个变量,我在模板中使用的助手在更新依赖变量时不会重新呈现,如 http://jsfiddle.net/ep76m4af/3/
中所示
下面是数据和模板助手定义:
var data = {
scenarios: scenarios,
selectedScenario: scenarios[0],
selectedInstances: [scenarios[0].instances[0]]
};
$.templates({
scenariosTemplate: {
markup: "#view",
helpers: {
isSelected: (function () {
function helper (instance) {
return (data.selectedInstances.indexOf(instance) > -1);
}
helper.depends = [data, "~root.selectedInstances"];
return helper;
})()
}
}
});
这是相关的模板代码:
{^{for selectedScenario^instances}}
<div data-link="{:name} selected{:~isSelected(#data)}"></div>
{{/for}}
使用
向data.selectedInstances添加实例时
$.observable(data.selectedInstances).insert(instance);
视图未相应更新。
我的代码中的“.depends”部分是不是不正确或有其他错误?
顺便说一句,我需要将选择保留在场景之外,因为场景将存储在数据库中并且选择仅在客户端。
将 ~root.selectedInstances 更改为 ~root.selectedInstances.length 解决了问题
helper.depends = [data, "~root.selectedInstances.length"];
我指出的一个助手依赖于我模型中的一个变量,我在模板中使用的助手在更新依赖变量时不会重新呈现,如 http://jsfiddle.net/ep76m4af/3/
中所示下面是数据和模板助手定义:
var data = {
scenarios: scenarios,
selectedScenario: scenarios[0],
selectedInstances: [scenarios[0].instances[0]]
};
$.templates({
scenariosTemplate: {
markup: "#view",
helpers: {
isSelected: (function () {
function helper (instance) {
return (data.selectedInstances.indexOf(instance) > -1);
}
helper.depends = [data, "~root.selectedInstances"];
return helper;
})()
}
}
});
这是相关的模板代码:
{^{for selectedScenario^instances}}
<div data-link="{:name} selected{:~isSelected(#data)}"></div>
{{/for}}
使用
向data.selectedInstances添加实例时$.observable(data.selectedInstances).insert(instance);
视图未相应更新。
我的代码中的“.depends”部分是不是不正确或有其他错误?
顺便说一句,我需要将选择保留在场景之外,因为场景将存储在数据库中并且选择仅在客户端。
将 ~root.selectedInstances 更改为 ~root.selectedInstances.length 解决了问题
helper.depends = [data, "~root.selectedInstances.length"];