下面是两个淘汰模板plugin和plugin2。两者都依赖于 ko.computed observables,并且两个 observables 的 deferEvaluation 属性都设置为 true。插件不渲染,但plugin2渲染。
需要更改哪些内容才能使插件呈现(保持 deferEvaluation==true)?
在这里摆弄 http://jsfiddle.net/jeljeljel/YKLGM/
HTML
<div id="plugin" data-bind="template: { name: 'template1', data: $data }" ></div>
<div id="plugin2" data-bind="template: { name: 'template2', data: $data }" ></div>
<script type="text/html" id="template1">
<div data-bind="foreach: columns()">
<span data-bind="text: displayText"></span>
</div>
</script>
<script type="text/html" id="template2">
<div data-bind="text: dataItem" ></div>
</script>
Javascript
var gridData = {
columns: [{
displayText: 'Name'
}, {
displayText: 'Last Login Date'
}, {
displayText: 'Email'
}]
};
function DataModel() {
var self = this;
self.columns = ko.observableArray([]);
self.loadGrid = ko.computed({
read: function () {
self.columns(gridData.columns);
},
owner: this,
deferEvaluation: true
});
self.id = ko.observable(1);
self.dataItem = ko.computed({
read: function () {
return self.id() * 3;
},
owner: this,
deferEvaluation: true
});
}
dataModel = new DataModel();
ko.applyBindings(dataModel);