0

我指出的助手取决于模型中的变量,并且我在模板中使用的助手在更新依赖的变量时不会重新呈现,如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”部分不正确还是有其他问题?

顺便说一句,我需要将选择排除在场景之外,因为场景将存储在数据库中,并且选择仅在客户端。

4

1 回答 1

0

将 ~root.selectedInstances 更改为 ~root.selectedInstances.length 解决了问题

 helper.depends = [data, "~root.selectedInstances.length"];

http://jsfiddle.net/ep76m4af/4/

于 2015-09-03T13:54:12.570 回答